Twig advanced
| Layouts with extends | |
| Blocks | |
| Common filters | |
| path() and asset() |
Layouts with extends
Pages share one layout instead of repeating the header and footer. The child template names its parent on the very first line:
{% extends "base.html.twig" %} <h1>Hello from the child page</h1>
Everything below that line normally lives inside blocks, which the next section covers.
Blocks
The layout declares named holes with block tags, and each page fills them with its own content:
{% block title %}My blog{% endblock %} {% block body %} <p>Page text goes here.</p> {% endblock %}
Common filters
Filters transform values with a pipe. They chain from left to right, so read them as a pipeline:
{{ title|upper }} {{ created|date("Y-m-d") }} {{ text|striptags|slice(0, 100) }}
path() and asset()
Functions build URLs and asset paths so templates never hardcode addresses. The path function takes a route name, and asset takes a file from under the public folder:
<a href="{{ path("app_hello") }}">Hello page</a> <img src="{{ asset("img/logo.png") }}">
Next: Doctrine setup
Article author: Arthur Isaev