1 2
<?php echo $var ?>
<?php echo htmlspecialchars($var, ENT_QUOTES, 'UTF-8') ?>
In comparison, Twig has a very concise syntax, which make templates more readable:
1 2 3
{{ var }}
{{ var|escape }}
{{ var|e }} {# shortcut to escape a variable #}
Template oriented syntax Twig has shortcuts for common patterns, like
having a default text displayed when you iterate over an empty array:
1 2 3 4 5
{% for user in users %}
* {{ user.name }}
{% else %}
No users have been found.
{% endfor %}
Full Featured: Twig supports everything you need to build powerful
templates with ease: multiple inheritance, blocks, automatic output-escaping,
and much more:
1 2 3 4 5
{% extends "layout.html" %}
{% block content %}
Content of the page...
{% endblock %}
●Easy to learn: The syntax is easy to learn and has been optimized to
allow web designers to get their job done fast without getting in their way.
Of course, PHP is also the language for which you can find the more
template engine projects. But most of them do not embrace web development best practices yet:
●Extensibility: Twig is flexible enough for all your needs, even the most
complex ones. Thanks to an open architecture, you can implement your own
language constructs (tags, filters, functions, and even operators) to create
your very own DSL.
●Unit tested: Twig is fully unit-tested. The library is stable and ready
to be used in large projects.
●Documented: Twig is fully documented, with a dedicated online
book, and of course a full API documentation.
Secure: When it comes to security, Twig has some unique features:
Automatic output escaping: To be on the safe side, you can enable
automatic output escaping globally or for a block of code:
1 2 3 4 5
{% autoescape "html" %}
{{ var }}
{{ var|raw }} {# var won't be escaped #}
{{ var|escape }} {# var won't be doubled-escaped #}
{% endautoescape %}
Sandboxing: Twig can evaluate any template in a sandbox environment
where the user has access to a limited set of tags, filters, and object
methods defined by the developer. Sandboxing can be enabled globally or
locally for just some templates:
1
{{ include('page.html', sandboxed = true) }}
●Clean Error Messages: Whenever you have a syntax problem within a template,
Twig outputs a helpful message with the filename and the line number where
the problem occurred. It eases the debugging a lot.
●Fast: One of the goals of Twig is to be as fast as possible.
To achieve the best speed possible, Twig compiles templates down to plain
optimized PHP code. The overhead compared to regular PHP code was reduced to
the very minimum.
Website powered by Symfony and Twig, deployed on