public function indexAction($count, $firstName) { return new Response(‘It\‘s a traaaaaaaap!‘); }
public function indexAction($count, $firstName) { return $this->render( ‘EventBundle:Default:index.html.twig‘, array(‘name‘ => $firstName) ); }
使用变量{{ name }}
也可以打印变量 <?php echo $name;?>{% for i in 1..count %} Hello <strong>{{ name }}</strong> # {{ i }}!<br/> {% endfor %}
注释:{# Hello comments! #}
过滤:
Hello <strong>{{ name|upper }}</strong>
模板继承:定义一个基础模板,app/Resources/views/base.html.twig
{# 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>
继承上面的模板:
{# src/Yoda/EventBundle/Resources/views/Default/index.html.twig #}
{% extends ‘::base.html.twig‘ %}
{% block body %}
{# ... the rest of the template ... #}
{% endblock %}
模板文件名由三部分组成:bundle,子文件夹,模板文件
在上面的模板例子中,bundle 和子文件夹是缺失的
看下面的例子:
/bundle/子目录/模板文件
EventBundle:Default:index.html.twig:src/Yoda/EventBundle/Resources/views/Default/index.html.twig
/bundle/模板文件
EventBundle::index.html.twig:src/Yoda/EventBundle/Resources/views/index.html.twig
/模板文件
::base.html.twig:app/Resources/views/index.html.twig
原文:http://www.cnblogs.com/webskill/p/5117238.html