{# == basics == #}
{# comment #}
{{ varname }}
{% doSomething %}
{{ varname|filtername }}
{# == inheritance == #}
{# a block defined in the parent: #}
{% block blockname %}
block content
{% endblock %}
{# can be overridden in child template: #}
{% extends 'parent.html.twig' %}
{% block blockname %}
new content
{# get content of parent's block #}
{{ parent() }}
{% endblock %}
{# create links using path() or url() and route name #}
Relative link
Absolute link
{# use asset() to get assets #}
===== Templates =====
==== Twig Template Caching ====
===== Template Inheritance and Layouts =====
===== Template Naming and Locations =====
==== Referencing Templates in a Bundle ====
==== Template Suffix ====
===== Tags and Helpers =====
==== Including other Templates ====
{% for article in articles %}
{{ include('article/article_details.html.twig', { 'article': article }) }}
{% endfor %}
==== Embedding Controllers ====
{# relative links with path(): #}
Home
Dynamic link
{# absolute links with url(): #}
Home
Dynamic link
==== Linking to Assets ====
Use the ''asset()'' function:
===== Including Stylesheets and JavaScripts in Twig =====
Consider using [[http://symfony.com/doc/current/cookbook/assetic/asset_management.html|Assetic]].
With Twig, assuming you have the parent:
{# ... #}
{% block stylesheets %}
{% endblock %}
{# ... #}
in its child you can add to the assets defined in the parent:
{% extends 'base.html.twig' %}
{% block stylesheets %}
{{ parent() }}
{% endblock %}
{# ... #}
You can also include assets located in your bundles' ''Resources/public'' folder. You will need to run ''php app/console assets:install target [--symlink]'', which moves (or symlinks) files into the correct location (target is by default "web".).
===== Global Template Variables =====
* ''app.user'': The current user object.
* ''app.request'': The request object.
* ''app.session'': The session object.
* ''app.environment'': The current environment (dev, prod, etc).
* ''app.debug'': True if in debug mode. False otherwise.
===== Configuring and Using the templating Service =====
return $this->render('article/index.html.twig');
is equivalent to:
use Symfony\Component\HttpFoundation\Response;
$engine = $this->container->get('templating');
$content = $engine->render('article/index.html.twig');
return $response = new Response($content);
It's preconfigured out of the box. Further config is in:
{{ entry.body }}
{% endfor %} {% endblock %}{{ article.body|raw }}
You can also disable output escaping inside a ''{% block %}'' area or for an entire template. See [[http://twig.sensiolabs.org/doc/api.html#escaper-extension|Output Escaping]] in the Twig documentation.
==== Output Escaping in PHP ====
In PHP, output escaping is not automatic, meaning you'll need to manually ''escape()'' where necessary:
Hello escape($name) ?> // HTML escapting by default
var myMsg = 'Hello escape($name, 'js') ?>'; // Specify context
===== Debugging =====
''dump()'' is your friend.
In PHP:
# By filename:
$ php app/console lint:twig path/to/your_tempalte.html.twig
# By directory:
$ php app/console lint:twig app/Resources/views
===== Template Formats =====
Twig can render *any** format.
* e.g.: XML ''article/index.xml.twig''.
The ''name.format.twig'' naming format is a convention. Using the convention you can do:
public function indexAction(Request $request)
{
$format = $request->getRequestFormat();
return $this->render('article/index.'.$format.'.twig');
}
(See the [[http://symfony.com/doc/current/book/routing.html#advanced-routing-example|Advanced Example]] in Routing.)
Create format links thus:
PDF Version