Warning, /frameworks/ktexttemplate/docs/for_themers.dox is written in an unsupported language. File is not indexed.

0001 namespace KTextTemplate
0002 {
0003 
0004 /**
0005 
0006   @page for_themers KTextTemplate for theme artists
0007 
0008   The syntax of %KTextTemplate templates is the same as Django templates. This page is an introduction to the syntax, but any Django template introduction is also relevant. Here's some good ones:
0009 
0010   - https://djangobook.com/mdj2-django-templates/
0011   - https://djangobook.com/extending-template-system/
0012   - http://docs.djangoproject.com/en/dev/topics/templates/
0013 
0014 
0015   @section template_syntax Syntax of KTextTemplate templates
0016 
0017   The syntax of %KTextTemplate templates contains four types of tokens - plain text, comments, variables and control tags.
0018 
0019   A simple template might look like this:
0020   @verbatim
0021     {# My simple Template #}
0022 
0023     Hello {{ person.name }},
0024 
0025     {% if person.hasBirthday %}
0026     Happy Birthday!
0027     {% else %}
0028     Have a nice day!
0029     {% endif %}
0030 
0031     Bye,
0032 
0033     {{ myname }}
0034   @endverbatim
0035 
0036   - Content inside <tt>{# #}</tt> are comments and are not part of the output.
0037   - Content inside <tt>{{ }}</tt> are variables which are substitued when the template is rendered.
0038   - Content inside <tt>{% %}</tt> are control tags which can affect the rendering of the template. %KTextTemplate ships with commonly used tags such as @gr_tag{if}, @gr_tag{for}, @gr_tag{cycle} and a host of others (see&nbsp;http://docs.djangoproject.com/en/dev/ref/templates/builtins/). It is also possible for application developers and artists to define their own tags.
0039 
0040 
0041   @subsection loops Loops
0042   The type of a variable determines how it can be used in templates. For example, treatment of items in an iterable context works as follows.
0043 
0044   Lists work as expected:
0045   @verbatim
0046     <h2>A few of my favourite things</h2>
0047     <ul>
0048     {% for item in mylist %}
0049     <li>{{ item }}</li>
0050     {% endfor %}
0051     </ul>
0052   @endverbatim
0053 
0054   If an <tt>int</tt> or <tt>double</tt> is used in a loop, it is treated as a list containing one item. That is, this
0055 
0056   @verbatim
0057     {% for item in myint %}
0058     <li>{{ item }}</li>
0059     {% endfor %}
0060   @endverbatim
0061 
0062   would be rendered as
0063   @verbatim
0064     <li>6</li>
0065   @endverbatim
0066 
0067   if <tt>myint</tt> is <tt>6</tt> in the context.
0068 
0069   @subsection truthiness Truthiness
0070 
0071   Truthiness of a variable is evaulated in a similar way to python. That is,
0072 
0073   - The invalid QVariant is <tt>false</tt>
0074   - An empty QString is <tt>false</tt>
0075   - 0 is <tt>false</tt>
0076   - An empty container such as QVariantList or QVariantHash is <tt>false</tt>
0077   - Boolean <tt>false</tt> is <tt>false</tt>
0078   - QObjects that define their own truth behaviour and return <tt>false</tt> are <tt>false</tt>
0079 
0080   Everything else is <tt>true</tt>.
0081 
0082   @verbatim
0083     {% if mylist %}
0084     <ul>
0085     {% for item in mylist %}
0086     <li>{{ item }}</li>
0087     {% endfor %}
0088     </ul>
0089     {% endif %}
0090   @endverbatim
0091 
0092   @subsection lookups Variable lookups
0093 
0094   So far we've mostly used simple variable lookups in @gr_var{variable} tags, but the template system is capable of a lot more. Complex structures can be evaluated using the dot (<tt>'.'</tt>) character.
0095 
0096   The dot can be used for list index lookup (Note that list lookup is <tt>0</tt>-indexed):
0097   @verbatim
0098     The first item is {{ mylist.0 }}, and the fifth item is {{ mylist.4 }}.
0099   @endverbatim
0100 
0101   It can also be used for QVariantHash key lookup:
0102 
0103   @verbatim
0104     The hash value is {{ myhash.mykey }}.
0105   @endverbatim
0106 
0107   And it can retrieve properties from QObject instances:
0108 
0109   @verbatim
0110     The object property is {{ myobj.myprop }}.
0111   @endverbatim
0112 
0113   @subsection filters Filters
0114 
0115   Filters can further affect the result of including a variable in a template. Filters are separated by the pipe (<tt>'|'</tt>) character.
0116 
0117   @verbatim
0118     Name is {{ name }}, and name in uppercase is {{ name|upper }}.
0119 
0120     // rendered as
0121     // Name is Alice, and name in uppercase is ALICE.
0122   @endverbatim
0123 
0124   Note that filters may be combined with dot-lookup. For example, if <tt>people</tt> is a list of <tt>Person</tt> objects,
0125   and <tt>Person</tt> objects have <tt>name</tt> properties:
0126 
0127   @verbatim
0128     First persons name: {{ people.0.name|upper }}.
0129 
0130     // rendered as
0131     // First persons name: BOB.
0132   @endverbatim
0133 
0134   Filters may also be chained. Here, 'Claire' is first uppercased, then lowercased:
0135 
0136   @verbatim
0137     Name is {{ name|upper|lower }}
0138 
0139     // rendered as
0140     // First persons name: claire.
0141   @endverbatim
0142 
0143   Filters may take one string argument, which is delimited by a colon (<tt>':'</tt>).
0144 
0145   If <tt>peoplestring</tt> contains <tt>"Dave and Ellen and Frank"</tt>, we can cut the string <tt>'and '</tt>
0146 
0147   @verbatim
0148     The people are {{ peoplestring|cut:"and " }}
0149 
0150     // rendered as
0151     // The people are Dave Ellen Frank.
0152   @endverbatim
0153 
0154   %KTextTemplate ships with many useful filters including <tt>upper</tt>, <tt>lower</tt>, <tt>slice</tt>, <tt>truncate</tt>, <tt>join</tt>, <tt>split</tt> etc (see&nbsp;http://docs.djangoproject.com/en/dev/ref/templates/builtins/). Application developers and artists can also define their own filters.
0155 
0156   @subsection template_loading Template Inclusion and Inheritance
0157 
0158   It is possible for templates to include other templates in two ways, by directly including the content of another template, and by extending another template.
0159 
0160   @subsubsection template_including
0161 
0162   The @gr_tag{include} tag is used to include the content of another template.
0163 
0164   @verbatim
0165     <h1>My page</h1>
0166       {% include "breadcrumbs.html" %}
0167 
0168     <h2>My Data</h2>
0169       {% include "table.html" %}
0170   @endverbatim
0171 
0172   If <tt>"breadcrumbs.html"</tt> and <tt>"table.html"</tt> exist and contain appropriate content, they will be rendered and added to the output of the template.
0173 
0174   @subsubsection template_extending
0175 
0176   The @gr_tag{extends} tag is used to include and override the content of another template.
0177 
0178   The template being extended must define several blocks using the @gr_tag{block} tag. Typically, one or more base templates will be defined which define the structure and content of all templates, and some placeholders for other templates to fill in.
0179 
0180   For example, a <tt>base.html</tt> might look like this:
0181 
0182   @verbatim
0183     <html>
0184       <head>
0185         <title>{% block title %}My Stuff{% endblock %}</title>
0186       </head>
0187       <body>
0188         <div class="sidebar">
0189           {% block sidebar %}
0190           {% endblock sidebar %}
0191         </div>
0192         <div class="main_content">
0193           {% block content %}
0194           {% endblock content %}
0195         </div>
0196       </body>
0197     </html>
0198   @endverbatim
0199 
0200   Then other templates could be written which extend this template, reusing its content and replacing the content of the @gr_tag{block} tags.
0201 
0202   For example, a page about books:
0203 
0204   @verbatim
0205     {% extends "base.html" %}
0206     {% block title %}{{ block.super }} - My Books{% endblock %}
0207 
0208     {% block content %}
0209     <ul>
0210       {% for book in books %}
0211         <li>{{ book.name }}, {{ book.author }}
0212       {% endfor %}
0213     </ul>
0214     {% endblock %}
0215   @endverbatim
0216 
0217   Or a page about DVDs:
0218 
0219   @verbatim
0220     {% extends "base.html" %}
0221     {% block title %}{{ block.super }} - My DVDs{% endblock %}
0222 
0223     {% block content %}
0224     <ul>
0225       {% for dvd in dvds %}
0226         <li>{{ dvd.title }}, {{ dvd.director }}
0227       {% endfor %}
0228     </ul>
0229     {% endblock content %}
0230   @endverbatim
0231 
0232   Note that it is optional to override a @gr_tag{block} in a extended template. It is also optional to repeat the name of the block in its corresponding @gr_tag{endblock} tag.
0233 
0234   The content of an overriden tag is available in the @gr_var{block.super} variable, and can be reused where appropriate. In the above examples, the use of @gr_var{block.super} results in the titles of the rendered pages being <tt>"My Stuff - My Books"</tt>, and <tt>"My Stuff - My DVDs"</tt> respectively.
0235 
0236   @section templates_safestring Autoescaping in templates.
0237 
0238   When creating HTML string output it is necessary to consider escaping data inserted into the template. HTML escaping involves replacing <tt>'&lt;'</tt> with <tt>'&amp;lt;'</tt> and <tt>'&amp;'</tt> with <tt>'&amp;amp;'</tt> etc. %KTextTemplate automatically escapes string input before adding it to the output.
0239 
0240   This is relevant when writing a variable from the context into the Template.
0241 
0242   If the context object <tt>companies</tt> is a list containing (<tt>'Burger King'</tt>, <tt>'Ben &amp; Jerries'</tt>, <tt>'Ford'</tt>), and it is used in a template such as:
0243 
0244   @verbatim
0245     <ul>
0246     {% for company in companies %}
0247       <li>{{ company }}</li>
0248     {% endfor %}
0249     </ul>
0250   @endverbatim
0251 
0252   The output would be
0253 
0254   @verbatim
0255     <ul>
0256       <li>Burger King</li>
0257       <li>Ben &amp; Jerries</li>
0258       <li>Ford</li>
0259     </ul>
0260   @endverbatim
0261 
0262   Notice that the <tt>'&amp;'</tt> has been replaced with <tt>'&amp;amp;'</tt>, as is appropriate for html output.
0263 
0264   Sometimes however, a variable will contain text which has already been escaped and does not need to be escaped again. For example,
0265   if we already created a <tt>table</tt> in the context containing the content:
0266 
0267   @verbatim
0268     <table class="myclass">
0269       <tr><th> Company </th><th> Product </th></tr>
0270       <tr><td> Burger King </td><td> Fast Food </td></tr>
0271       <tr><td> Ben &amp; Jerries </td><td> Icecream </td></tr>
0272       <tr><td> Ford </td><td> Cars </td></tr>
0273     </table>
0274   @endverbatim
0275 
0276   and a template with the content:
0277 
0278   @verbatim
0279     <h2> Table of companies </h2>
0280 
0281     {{ table }}
0282 
0283     As you can see in the table...
0284   @endverbatim
0285 
0286   the content would not be rendered properly because it would be escaped.
0287 
0288   @verbatim
0289     <h2> Table of companies </h2>
0290 
0291     &lt;table class=&quot;myclass&quot;&gt;
0292       &lt;tr&gt;&lt;th&gt; Company &lt;/th&gt;&lt;th&gt; Product &lt;/th&gt;&lt;/tr&gt;
0293       &lt;tr&gt;&lt;td&gt; Burger King &lt;/td&gt;&lt;td&gt; Fast Food &lt;/td&gt;&lt;/tr&gt;
0294       &lt;tr&gt;&lt;td&gt; Ben &amp;amp; Jerries &lt;/td&gt;&lt;td&gt; Icecream &lt;/td&gt;&lt;/tr&gt;
0295       &lt;tr&gt;&lt;td&gt; Ford &lt;/td&gt;&lt;td&gt; Cars &lt;/td&gt;&lt;/tr&gt;
0296     &lt;/table&gt;
0297 
0298     As you can see in the table...
0299   @endverbatim
0300 
0301   Note that the content of <tt>table</tt> has already been escaped. That is <tt>'Ben &amp;amp; Jerries'</tt> is already used instead of <tt>'Ben &amp; Jerries'</tt>. If a variable has already been escaped and should not be escaped again, it can be marked as safe from further escaping using the <tt>safe</tt> filter.
0302 
0303   @verbatim
0304     <h2> Table of companies </h2>
0305 
0306     {{ table|safe }}
0307 
0308     As you can see in the table...
0309   @endverbatim
0310 
0311   Resulting in:
0312 
0313   @verbatim
0314     <h2> Table of companies </h2>
0315 
0316     <table class="myclass">
0317       <tr><th> Company </th><th> Product </th></tr>
0318       <tr><td> Burger King </td><td> Fast Food </td></tr>
0319       <tr><td> Ben &amp; Jerries </td><td> Icecream </td></tr>
0320       <tr><td> Ford </td><td> Cars </td></tr>
0321     </table>
0322 
0323     As you can see in the table...
0324   @endverbatim
0325 
0326   It is also possible to turn this autoescaping feature off for a block in a template.
0327 
0328   For example:
0329 
0330   @verbatim
0331     <h2> Some pre-prepared safe data </h2>
0332 
0333     {% autoescape off %}
0334       {{ table }}
0335       As you can see in the table...
0336 
0337       {% for list in lists %}
0338         {{ list }}
0339       {% endfor %}
0340 
0341       {{ paragraph_data }}
0342     {% endautoescape %}
0343   @endverbatim
0344 
0345   would not escape the content between the <tt>autoescape</tt> and <tt>endautoescape</tt> tags. This should only be used for content which is actually already safe.
0346 
0347   @see https://docs.djangoproject.com/en/1.9/ref/templates/language/#for-individual-variables
0348 
0349   @subsection extending_syntax Extending the syntax
0350 
0351   It is also possible to extend the syntax of %KTextTemplate as you need it using Javascript if the application developer chooses. See @ref javascript_libraries for more. This is considered an advanced topic.
0352 
0353 */
0354 
0355 }