Symfony Templates Cookbook: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
|||
| Line 20: | Line 20: | ||
== Headers and footers == | == Headers and footers == | ||
Headers and footers are located in base templates. | |||
Child templates extend the base template to use the header and footers.<ref>[http://symfony.com/doc/current/book/templating.html#template-inheritance-and-layouts Template Inheritance and Layouts], Symfony documentation</ref> | |||
A base template can contain default content. If the child template doesn't define a block that exists in the parent template, and the parent template contains default content in the block, the content from the parent template will be used. | |||
Base template: | |||
<syntaxhighlight lang="html4strict" highlight="6,10-15,19"> | |||
{# app/Resources/views/base.html.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> | |||
</syntaxhighlight> | |||
Child template: | |||
<syntaxhighlight lang="html4strict" highlight="2,4,6"> | |||
{# app/Resources/views/Blog/index.html.twig #} | |||
{% extends 'base.html.twig' %} | |||
{% block title %}My cool blog posts{% endblock %} | |||
{% block body %} | |||
{% for entry in blog_entries %} | |||
<h2>{{ entry.title }}</h2> | |||
<p>{{ entry.body }}</p> | |||
{% endfor %} | |||
{% endblock %} | |||
</syntaxhighlight> | |||
== Notes == | == Notes == | ||
<references/> | <references/> | ||
Revision as of 16:34, 25 January 2015
Template tags
`{{ ... }} enclose variables. Object properties are referenced with dot notation: {{obj.prop}}. Functions can be put in tags too.
Filters can be applied with the pipe character: {{ title|upper }}.
{% ... %}` encloses logic.
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
</ul>
{# ... #} encloses comments.[1]
Headers and footers are located in base templates.
Child templates extend the base template to use the header and footers.[2]
A base template can contain default content. If the child template doesn't define a block that exists in the parent template, and the parent template contains default content in the block, the content from the parent template will be used.
Base template:
{# app/Resources/views/base.html.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>
Child template:
{# app/Resources/views/Blog/index.html.twig #}
{% extends 'base.html.twig' %}
{% block title %}My cool blog posts{% endblock %}
{% block body %}
{% for entry in blog_entries %}
<h2>{{ entry.title }}</h2>
<p>{{ entry.body }}</p>
{% endfor %}
{% endblock %}
Notes
- ↑ Creating and Using Templates, Symfony documentation
- ↑ Template Inheritance and Layouts, Symfony documentation