KDevelop5/File template specification

    From KDE TechBase
    Under Construction
    This is a new page, currently under construction!

    Template language

    Source filj templates use the Grantlee library for rendering templates. It has more features than KMacroExpander, the most important being loops and the ability to expose custom data types from C++ code to templates. The use of Grantlee makes templates more powerful, but also more difficult to write.

    For information regarding Grantlee templates in general, refer to its documentation. Since Grantlee's template language is based on Django's, it might be useful to read the Django template documentation as well.

    Example template template

    There is a project template that creates a new project consisting of a single KDevelop class template. You can use it by starting a new project, choosing Get More Templates... and installing this one. Proceeding to create a project using this template will create an example class template.

    File structure

    Like project templates, file templates are compressed directories, and they too contain a special description file and any number of content files. It is recommended that the description file has a .desktop extension instead of .kdevtemplate, although both are supported.

    Unlike with project templates, not all files from the archive are copied to the output directory. Because Grantlee supports template inheritance and inclusions, file templates may contain helper files that only serve for convenience and produce no actual output. The actual output files have to be specified in the template description.

    Description file format

    The template descriptions files are normal .desktop files. They must contain a General section, as well as one section for each output file. In most cases, the template will have two output files for C++ classes and one output file for other languages. However, using separate user interface files and/or a shared d-pointer hierarchy can cause a class to need more files. Templates in KDevelop allow any number of output files.


    An example description for a template with three output files is below

    Name=Private Pointer
    Comment=C++ Class with a private D-pointer
    Name=Public Header
    OutputFile={{ name }}.h
    Name=Private Header
    OutputFile={{ name }}_p.h
    OutputFile={{ name }}.cpp


    The Name and Comment entries are the same as in other .desktop files and can be translated.

    The Category is used for grouping when showing a list of templates to the user. It should contain at least two levels, separated by slashes. The default templates use the following format: Language/Framework, e.g. C++/Qt.

    The Type is optional and can be either Class or Test. In both cases the wizard will add some additional configuration pages and variables.

    The Language is optional and can reference a KDevelop language plugin by it's X-KDevelop-Language identifier. When given, the language plugin will be asked to create a class helper, otherwise a default class helper will be chosen.

    The OptionsFile entry is optional, using it specifies configuration options for this template. For more information about template options, see Custom options.

    A class template description file may also contain an entry with key BaseClasses. It is only used for class templates, and any clases specified in this way are added by default to the inheritance list for the new class. The user can still remove those base classes, or add them manually, so this entry is entirely optional, it only makes things easier for the user. It accepts a list of inheritance statements, separated by commas, like so

    ; Example for a Qt object
    BaseClasses=public QObject
    ; Example for a KDevelop plugin that implements an interface
    BaseClasses=public KDevelop::IPlugin,public KDevelop::IBasicVersionControl
    ; Example for a Python new-style class

    Output files

    The Files entry specifies template output files. It is a list of strings, where each element is the same as a name of a group in the description file. The corresponding group has three required entries

    • Name is the user visible file name. It can be translated.
    • File is the path to the input file within the template archive
    • OutputFile is the suggested output file name. The actual name can be set by the user, but this is the default value. It will be converted to lowercase if needed, so don't do in here.

    The description file above results in a dialog page such as this

    KDevelop template output files dialog
    Assistant page for template output files



    The following variables are passed to all templates, regardless of their type

    Variable name Type Description Example value
    license String The license header without any comment characters. It can be split into multiple lines. One of the headers listed in KDE Licensing Policy
    output_file_foo String The relative path to the output file with identifier foo mynewclass.h
    output_file_foo_absolute String The absolute path to the output file foo /home/user/projects/myproject/mynewclass.h

    Additionally, values of any custom options exposed by the template are passed as variables.


    The following variables are passed to Class templates

    Variable name Type Description Example value
    name String Class name without namespaces Example
    identifier String The full identifier of the new class, which is composed of namespaces and class name MyProject::Utilities::Example
    namespaces StringList List of nested namespaces in which the class is declared [MyProject, Utilities]
    description ClassDescription An object containing the class name, base classes, functions and members
    baseClasses InheritanceDescriptionList All direct base classes of the new class, containing both the inheritance type and the base class name.
    functions FunctionDescriptionList List of all the functions in the new class
    members VariableDescriptionList List of all the member variables in the new class

    The variables which are passed to class templates are also described in the Expression error: Unrecognized word "extragear". API documentation.

    C++ Specific variables

    Templates for classes in C++ may make use of some additional variables provided by the language support plugin. They cover finding include files and grouping class functions and members by access specifiers.

    Variable name Type Description
    public_functions, protected_functions, private_functions FunctionDescriptionList Class functions grouped by access modifiers. These lists do not include slots.
    public_slots, protected_slots, private_slots FunctionDescriptionList Qt slots grouped by access modifiers
    signals FunctionDescriptionList All Qt signals in the new class
    public_members, protected_members, private_members VariableDescriptionList Data members grouped by access modifiers
    needs_qobject_macro bool true if the class has at least one signal or slot, false otherwise
    included_files StringList List of headers files that need to be included. This variable lists the missing include files for base classes.


    The following variables are passed to Test templates

    Variable name Type Description Example value
    name String Test name without namespaces MathTest
    testCases StringList List of test case names. They do not necessarily begin or end with "test", it is up to templates for frameworks that require such prefixes to add them. [addition, multiplication, negativeNumbers]

    Additional templates and filters

    To make writing templates easier, KDevelop supplies additional template utilities. They are both in the form of text template, available for including into your own, or additional Grantlee filters.

    Templates for inclusion

    These templates usually read some variables from the context. Thus, you have to make sure the correct variables are set before including the template. This is normally done using with statements.

    {% for f in functions %}
      {% with f.arguments as arguments %}
        {{ f.returnType|default:"void" }} {{ f.name }}({% include "arguments_types_names.txt" %});
      {% endwith %}
    {% endfor %}

    Includable templates in KDevPlatform are:

    Template Required variables Language Description
    arguments_names.txt arguments : VariableDescriptionList Weakly-typed languages Comma-separated list of argument names.
    arguments_types_names.txt arguments : VariableDescriptionList Strongly-typed languages Comma-separated list of argument types and names.
    include_guard_cpp.txt namespaces : StringList C++ Include guard macro, generated from the namespaces and the class name.
    namespace_open_cpp.txt namespaces : StringList C++ Opening tag for nested namespaces
    namespace_close_cpp.txt namespaces : StringList C++ Closing tag for nested namespaces
    namespace_use_cpp.txt namespaces : StringList C++ using namespace statement for nested namespaces

    Template filters

    KDevPlatform provides an additional filter library. Because filters are implemented in code and not modifiable by users, it is preferred to use template inclusion if possible. These filters are mostly utilities for string operations and are not language specific.

    To use them in templates, add {% load kdev_filters %} to the beginning of your template file.

    Template filter Description
    lines_prepend Takes a string argument and prepends it to every line of the input. It is intended for licenses and other blocks of commented text.
    upper_first Converts the first character of input into uppercase, and leaves the rest intact. This is useful for creating accessor functions, and prefixing identifier in general.
    camel_case Converts the input from an underscore notation to camel case, starting with a lowercase character.
    camel_case_upper Converts the input from an underscore notation to camel case, starting with an uppercase character.
    underscores Converts the input to the underscore notations, where words are separated by underscores instead of uppercase letters. The output is entirely lowercase.

    Custom options

    Templates can expose configuration options to the user prior to the class generation. To do this, add a KConfig XT formatted file into the archive, and specify an OptionsFile entry in the template description file pointing to it.

    Template options are meant to be used only for small differences in large, complex templates. To reduce the number of necessary clicks, it is preferred to use separate templates instead.

    For information about the options file format, see Using KConfig XT. Note that KDevelop only supports a subset of the KConfig XT specification. Only Int, Bool and String types are recognized, options with other types are ignored. Furthermore, only name, type, label and default attributes are used. The default value of options is rendered as a template, so it can include variables.

    Here is an example options file that exposes two string options, useful for private pointers

    <?xml version="1.0" encoding="UTF-8"?>
    <kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
      <kcfgfile arg="true"/>
      <group name="Private Class">
        <entry name="private_class_name" type="String">
          <label>Private class name</label>
          <default>{{ name }}Private</default>
        <entry name="private_member_name" type="String">
          <label>Private member name</label>

    Just before creating the class, KDevelop will show an assistant dialog page, asking for the two options

    KDevelop template options dialog
    Assistant page for template options

    The values the user sets for the two options will be available to the template as variables with names matching the entry names. In the private pointer example, the d-pointer declaration template might look like this

        {{ private_class_name }}* const {{ private_member_name }};

    If the user accepts the default values, and the class name is Example, the rendered result of this snippet will be

        ExamplePrivate* const d;