home.html.twig

You can obtain the result by requesting the url “http://localhost:8000/student/home”.

By default, Twig comes with a long list of tags, filters, and functions. Let’s go through one by one in detail.

Tags

Twig supports the following important tags −

Do

The do tag performs similar functions as regular expression with the exception that it doesn't print anything. Its syntax is as follows −

{% do 5 + 6 %}

Include

The include statement includes a template and returns the rendered content of that file into the current namespace. Its syntax is as follows −

{% include 'template.html' %}

Extends

The extends tag can be used to extend a template from another one. Its syntax is as follows −

{% extends "template.html" %}

Block

Block acts as a placeholder and replaces the contents. Block names consists of alphanumeric characters and underscores. For example,

<title>{% block title %}{% endblock %}</title>

Embed

The embed tag performs a combination of both include and extends. It allows you to include another template's contents. It also allows you to override any block defined inside the included template, such as when extending a template. Its syntax is as follows −

Dump

The dump function dumps information about a template variable. For example,

{{ dump(user) }}

Max

The max function returns the largest value of a sequence. For example,

{{ max(1, 5, 9, 11, 15) }}

Min

The min function returns the smallest value of a sequence. For example,

{{ min(1, 3, 2) }}

Include

The include function returns the rendered content of a template. For example,

{{ include('template.html') }}

Random

The random function generates a random value. For example,

{{ random([‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’]) }}
{# example output: Jan #}

Range

Range function returns a list containing an arithmetic progression of integers. For example,

{% for x in range(1, 5) %}
{{ x }},
{% endfor %}

It will produce the following result −

1,2,3,4,5

Layouts

A Layout represents the common parts of multiple views, i.e. for example, page header, and footer.

Template Inheritance

A template can be used by another one. We can achieve this using template inheritance concept. Template inheritance allows you to build a base “layout” template that contains all the common elements of web site defined as blocks.

Let’s take a simple example to understand more about template inheritance.

Example

Consider the base template located at “app/Resources/views/base.html.twig”. Add the following changes in the file.

Here, the {% extends %} tag informs the templating engine to first evaluate the base template, which sets up the layout and defines the block. The child template is then rendered. A child template can extend the base layout and override the title block. Now, request the url “http://localhost:8000” and you can obtain its result.

Assets

The Asset manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files, and image files.