发布于 2015-08-27 16:21:08 | 501 次阅读 | 评论: 0 | 来源: 网络整理
After reading the first part of this tutorial, you have decided that Symfony was worth another 10 minutes. In this second part, you will learn more about Twig, the fast, flexible, and secure template engine for PHP applications. Twig makes your templates more readable and concise; it also makes them more friendly for web designers.
The official Twig documentation is the best resource to learn everything about this template engine. This section just gives you a quick overview of its main concepts.
A Twig template is a text file that can generate any type of content (HTML, CSS, JavaScript, XML, CSV, LaTeX, etc.) Twig elements are separated from the rest of the template contents using any of these delimiters:
{{ ... }}
{% ... %}
for
loops and if
statements.{# ... #}
Below is a minimal template that illustrates a few basics, using two variables page_title
and navigation
, which would be passed into the template:
<!DOCTYPE html> <html> <head> <title>{{ page_title }}</title> </head> <body> <h1>{{ page_title }}</h1> <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.url }}">{{ item.label }}</a></li> {% endfor %} </ul> </body> </html>
To render a template in Symfony, use the render
method from within a controller. If the template needs variables to generate its contents, pass them as an array using the second optional argument:
$this->render('default/index.html.twig', array( 'variable_name' => 'variable_value', ));
Variables passed to a template can be strings, arrays or even objects. Twig abstracts the difference between them and lets you access “attributes” of a variable with the dot (.
) notation. The following code listing shows how to display the content of a variable passed by the controller depending on its type:
{# 1. Simple variables #} {# $this->render('template.html.twig', array('name' => 'Fabien') ) #} {{ name }} {# 2. Arrays #} {# $this->render('template.html.twig', array('user' => array('name' => 'Fabien')) ) #} {{ user.name }} {# alternative syntax for arrays #} {{ user['name'] }} {# 3. Objects #} {# $this->render('template.html.twig', array('user' => new User('Fabien')) ) #} {{ user.name }} {{ user.getName }} {# alternative syntax for objects #} {{ user.name() }} {{ user.getName() }}
More often than not, templates in a project share common elements, like the well-known header and footer. Twig solves this problem elegantly with a concept called “template inheritance”. This feature allows you to build a base template that contains all the common elements of your site and defines “blocks” of contents that child templates can override.
The index.html.twig
template uses the extends
tag to indicate that it inherits from the base.html.twig
template:
{# app/Resources/views/default/index.html.twig #} {% extends 'base.html.twig' %} {% block body %} <h1>Welcome to Symfony!</h1> {% endblock %}
Open the app/Resources/views/base.html.twig
file that corresponds to the base.html.twig
template and you’ll find the following Twig code:
{# app/Resources/views/base.html.twig #} <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>{% block title %}Welcome!{% endblock %}</title> {% block stylesheets %}{% endblock %} <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" /> </head> <body> {% block body %}{% endblock %} {% block javascripts %}{% endblock %} </body> </html>
The {% block %}
tags tell the template engine that a child template may override those portions of the template. In this example, the index.html.twig
template overrides the body
block, but not the title
block, which will display the default content defined in the base.html.twig
template.
Twig is simple yet powerful. Thanks to layouts, blocks, templates and action inclusions, it is very easy to organize your templates in a logical and extensible way.
You have only been working with Symfony for about 20 minutes, but you can already do pretty amazing stuff with it. That’s the power of Symfony. Learning the basics is easy, and you will soon learn that this simplicity is hidden under a very flexible architecture.
But I’m getting ahead of myself. First, you need to learn more about the controller and that’s exactly the topic of the next part of this tutorial. Ready for another 10 minutes with Symfony?