ArticleslgStudy

computer science

Twig (template engine)

Twig (template engine) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Twig (template engine) rather than just read about it. In short: Twig is a free and open-source template engine for PHP. Its syntax originates from Jinja and Django templates.

Key takeaways

  • Twig (template engine) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Twig (template engine) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Twig (template engine) from memory before moving on to harder problems.

Reference excerpt

Twig is a free and open-source template engine for PHP. Its syntax originates from Jinja and Django templates. It is licensed under the BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher. Symfony comes with bundled support for Twig as its default template engine since version 2. The same template language is used by the Nunjucks template engine, thus Nunjucks is also supported by the following tools.

Features Complex control flow Automatic escaping Template inheritance Variable filters i18n support (gettext) Macros Fully extendable Twig is supported by the following integrated development environments:

Eclipse via the Twig plugin Komodo and Komodo Edit via the Twig highlight/syntax check mode NetBeans via the Twig syntax plugin (until 7.1, native as of 7.2) PhpStorm (native as of 2.1) IntelliJ IDEs, including WebStorm, via a plugin Twig is supported by the following text editors:

Atom via the PHP-twig for atom Emacs via web-mode.el Notepad++ via the Notepad++ Twig Highlighter Sublime Text via the Twig bundle TextMate via the Twig bundle Vim via the Jinja syntax plugin or the vim-twig plugin Brackets via Brackets Twig Visual Studio Code via the Twig extension GTKSourceView via the Twig language definition Coda via the Twig syntax mode Coda 2 via the other Twig syntax mode SubEthaEdit via the Twig syntax mode

Syntax Twig defines three kinds of delimiters:

{{ ... }} is used to print the content of variables or the result of evaluating an expression. {# ... #} is used to add comments in the templates. These comments are not included in the rendered page. {% ... %} is used to execute statements, such as for-loops. For example: {% set foo = 'bar' %}, to assign. {% if i is defined and i == 1%} ... {% endif %}: condition. {% for i in 0..10 %} ... {% endfor %}: counter in a loop. Unlike in base PHP, Twig variables do not start with a dollar sign. The apostrophe (') is the escape character. To create an iterative array:

An associative array:

Operators precedence The operators precedence is, from the less to more priority:

Filters The filters provide some treatments on an expression, when placed after it, separated by pipes. For example:

capitalize: changes a string's first letter to capital. upper: changes a whole string to capital. first: displays the first line of an array. length: returns a variable size.

Special variables loop contains the current loop information. For example loop.index corresponds to the number of iterations which have already occurred. The global variables begin with underscores. For example: _route (URL part located after the domain) _self (current file name) So, to the a page route: {{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }} The CGI environment variables, such as {{ app.request.server.get('SERVER_NAME') }}.

Example The example below demonstrates some basic features of Twig.

Real world usage Twig has become a widely adopted templating engine in the PHP ecosystem. It is the default templating language for the Symfony framework and is also integrated into several content management systems (CMS) and applications, including:

Drupal adopted Twig as its default template engine starting from version 8, replacing PHPTemplate. CraftCMS uses Twig as its sole templating language, allowing developers to create highly customised front-end experiences. eZ Platform (Ibexa) integrates Twig for rendering templates within its content management system. Bolt CMS uses Twig for theming and front-end rendering. ShopWired uses Twig for theming and front-end rendering. OpenCart uses Twig for theming and HTML rendering. Twig is also employed in many custom PHP applications where secure and readable templating are important. The similarity of Twig to Jinja and Django templates has also made it familiar to developers coming from Python and other similar ecosystems.

See also

Smarty

References

External links Twig official website Fabien Potencier (2009-10-07). "Templating Engines in PHP". Retrieved 2023-10-27.

Worked examples

Example 1 — a first encounter with Twig (template engine)

Start with the simplest possible case. Write down what Twig (template engine) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Twig (template engine) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Twig (template engine) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Twig (template engine)

In research
Twig (template engine) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Twig (template engine) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Twig (template engine) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Drupal, Free software programmed in PHP, PHP libraries, so understanding it makes those chapters shorter.
In everyday life
Look for Twig (template engine) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Twig (template engine)” →

How to study Twig (template engine) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Twig (template engine) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Twig (template engine) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Twig (template engine) in simple terms?

Twig is a free and open-source template engine for PHP. Its syntax originates from Jinja and Django templates.

Why does Twig (template engine) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Twig (template engine)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Twig (template engine).

Tags

  • Drupal
  • Free software programmed in PHP
  • PHP libraries
  • Template engines

Keep exploring