Templates

A template is simply a text file that can generate any text-based format (HTML, XML, CSV, LaTeX ...). The most familiar type of template is a PHP template - a text file parsed by PHP that contains a mix of text and PHP code:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to Symfony!</title>
</head>
<body>
<h1><?php echo $page_title ?></h1>
<ul id="navigation">
<?php foreach ($navigation as $item): ?>
<li>
<a href="<?php echo $item->getHref() ?>">
<?php echo $item->getCaption() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>

But Symfony2 packages an even more powerful templating language called Twig1. Twig allows you to write concise, readable templates that are more friendly to web designers and, in several ways, more powerful than PHP templates:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to Symfony!</title>
</head>
<body>
<h1>{{ page_title }}</h1>
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
</ul>
</body>
</html>

Twig defines two types of special :
syntax :
{{ ... }} : "Says something": prints a variable or the result of an expression to the template.

{% ... %} : "Does something": a tag that controls the logic of the template; it is used to execute statements such as for-loops for example :

{{ title|lower }}

Why Twig?

  • Twig templates are meant to be simple and won't process PHP tags.

  • This is by design: the Twig template system is meant to express presentation, not program logic.

  • Twig can also do things that PHP can't, such as whitespace control, sandboxing, automatic and contextual output escaping, and the inclusion of custom functions and filters that only affect templates.

  • Twig contains little features that make writing templates easier and more concise.
Example :
<ul>
{% for user in users if user.active %}
<li>{{ user.username }}</li>
{% else %}
<li>No users found</li>
{% endfor %}
</ul>
				

Twig Template Caching


  • Twig is fast.

  • Each Twig template is compiled down to a native PHP class that is rendered at runtime.

  • The compiled classes are located in the app/cache/{environment}/twig directory (where {environment} is the environment, such as dev or prod) and in some cases can be useful while debugging.

  • a Twig template will be automatically recompiled when changes are made to it.

  • This means that during development you can happily make changes to a Twig template and instantly see the changes without needing to worry about clearing any cache.

Template Inheritance and Layouts


  • In Symfony2, this problem is thought about differently: a template can be decorated by another one. This works exactly the same as PHP classes: template inheritance allows you to build a base "layout" template that contains all the common elements of your site defined as blocks (think "PHP class with base methods").

  • A child template can extend the base layout and override any of its blocks (think "PHP subclass that overrides certain methods of its parent class").

Base layout file:
{# app/Resources/views/base.php.twig #}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{% block title %}Test Application{% endblock %}</title>
</head>
<body>
<div id="sidebar">
{% block sidebar %}
<ul>
<li><a href="/">Home</a></li>
			
<li><a href="/blog">Blog</a></li>
</ul>
{% endblock %}
</div>
<div id="content">
{% block body %}{% endblock %}
</div></body>
</html>