Идет загрузка
Main pagengcmsNews ngcms Introduction to TWIG

Introduction to TWIG

TWIG template system is being heavily discussed so let's find all the advantages and minuses to help you make choose between the default template system or the TWIG engine.

40.jpg (19.33 Kb)

The default template system has a lot of serious disadvantages:
* templates are interpreting as a text (design's code is not separated with variables), it means that it needs about 200-600 symbol replaces and 100-200 regular expressions based replaces, so processing of a one template takes some resources
* interpreting mode - processing of every template takes heaps of time
* cascade variables processing - using symbol replacement function may cause unexpected aftermath at processing variables inside other variables (for example, "{mainblock}" in the article’s body may make such an effect)
* if-blocks are represented with regular expressions; available if-conditions are hardcoded into the core or plugin; big data arrays are processed very slowly
* every repeatable actions require a new template because loops has not been implemented yet

TWIG's advantages:
* compiling mode - the first processing of a new-written template takes some time, but all the following ones are much faster
* module mode - slow compiling mode is combined with fast view module to reach maximum performance
* text and variables separating - template processing does not bring additional problems
* if-blocks and custom complex conditions can be used in any plugin with special flags activated
* loops - simple repeated actions can be done inside the template

The TWIG's possibilities are very high, so not everyone will use all the functions and features.

General syntax
1) Variables format.
Old syntax: {variableName}
New syntax: {{ variableName }}

2) If-blocks.
Old syntax: [if-logged] your_text [/if-logged]
New syntax: {% if (user.flags.logged) %} your_text {% endif %}

3) Simple loops.
Old syntax:
* main template: {entries}
* additional template: name: {name}, entries: {count} <br/>
New syntax:
{% for entry in entries %}
name {{ entry.name }}, entries: {{ entry.count }} <br/>
{% endfor %}

4) Display a block if some plugin is activeted (for example, xfields)::
Old syntax: [isplugin xfields]...[/isplugin]
New syntax: {% if pluginIsActive('xfields') %}...{% endif %}

Using global variables
Global variables can help you to show some information in all templates.

Examples of use:
1. Shows username if user is logged in or "guest" if not:
Hi, {% if (user.flags.logged) %}<b>{{user.name}}</b>{% else %}guest{% endif %}!

2. A block displaying especially for John
{% if (user.flags.logged and (user.name == 'John')) %}Hi, John!{% endif %}
A specially modified for NGCMS version of TWIG lets leave all site's templates without editing.
If you need additional functions start using TWIG for particular templates.

Комментирование данной новости запрещено.
Login with loginza
0.9.3 Release
28 октября 2011
Download actual version Next Generation CMS

Recent news

RSS не доступен