Connect with us


how to use template in symfony


how to use template in symfony








How to use template in symfony

Controller is responsible for handling each request that comes into a Symfony application. In reality, the controller delegates most of the heavy work to other places so that code can be tested and reused. When a controller needs to generate HTML, CSS or any other content, it hands the work off to the templating engine.

Including partials and components
{% include “Bundle:Controller:action” %}
in Symfony 2.1:
{% render “Bundle:Controller:action” with {“max” : 3} %}
in Symfony 2.2:
{{ render(controller(“Bundle:Controller:action”, {max :3})) }}

Global TWIG variables
app.request.get(‘foo’) //get
app.request.request.get(‘foo’) //post

<a href=”{{ path(‘homepage’) }}”>Home<a/> //relative
<a href=”{{ url(‘homepage’) }}”>Home<a/> //absolute
<a href=”{{ path(‘show’, {‘id’}) }}”>Home</a>

Debug variables in a template
{{ dump(article) }}

<img src=”{{ ‘uploads/’~yourown.url }}”/>


twig tags


When a template uses inheritance and if you want to print a block multiple times, use the block function:

<title>{% block title %}{% endblock %}</title>
<h1>{{ block(‘title’) }}</h1>
{% block body %}{% endblock %}


Inside code blocks you can also assign values to variables. Assignments use the set tag and can have multiple targets:

{% set foo = ‘foo’ %}
{% set foo = [1, 2] %}
{% set foo = {‘foo’: ‘bar’} %}
{% set foo = ‘foo’ ~ ‘bar’ %}
{% set foo, bar = ‘foo’, ‘bar’ %}

Filter sections allow you to apply regular Twig filters on a block of template data. Just wrap the code in the special filter section:

{% filter upper %}
This text becomes uppercase
{% endfilter %}
You can also chain filters:

{% filter lower|escape %}
<strong>SOME TEXT</strong>
{% endfilter %}



Loop over each item in a sequence. For example, to display a list of users provided in a variable called users:

{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
The loop variable

Inside of a for loop block you can access some special variables:

Variable Description
loop.index The current iteration of the loop. (1 indexed)
loop.index0 The current iteration of the loop. (0 indexed)
loop.revindex The number of iterations from the end of the loop (1 indexed)
loop.revindex0 The number of iterations from the end of the loop (0 indexed)
loop.first True if first iteration
loop.last True if last iteration
loop.length The number of items in the sequence
loop.parent The parent context
{% for user in users %}
{{ loop.index }} – {{ user.username }}
{% endfor %}



When a template uses inheritance, it’s possible to render the contents of the parent block when overriding a block by using the parent function:

{% extends “base.html” %}

{% block sidebar %}
<h3>Table Of Contents</h3>

{{ parent() }}
{% endblock %}


Macros are comparable with functions in regular programming languages. They are useful to put often used HTML idioms into reusable elements to not repeat yourself.

Here is a small example of a macro that renders a form element:

{% macro input(name, value, type, size) %}
<input type=”{{ type|default(‘text’) }}” name=”{{ name }}” value=”{{ value|e }}” size=”{{ size|default(20) }}” />
{% endmacro %}
Macros differs from native PHP functions in a few ways:

Default argument values are defined by using the default filter in the macro body;
Arguments of a macro are always optional.
But as PHP functions, macros don’t have access to the current template variables.

Macros can be defined in any template, and need to be “imported” before being used (see the documentation for the import tag for more information):

{% import “forms.html” as forms %}
The above import call imports the “forms.html” file (which can contain only macros, or a template and some macros), and import the functions as items of the forms variable.

The macro can then be called at will:

<p>{{ forms.input(‘username’) }}</p>
<p>{{ forms.input(‘password’, null, ‘password’) }}</p>
If macros are defined and used in the same template, you can use the special _self variable to import them:

{% import _self as forms %}
<p>{{ forms.input(‘username’) }}</p>


The if statement in Twig is comparable with the if statements of PHP.

In the simplest form you can use it to test if an expression evaluates to true:

{% if online == false %}
<p>Our website is in maintenance mode. Please, come back later.</p>
{% endif %}

Everything inside raw tags won’t be parsed.

{% raw %}
This variable {{foo}} won’t be parsed as twig var.
{% endraw. %}

Continue Reading
You may also like...

More in symfony


To Top