跳至主要内容

博文

目前显示的是标签为“twig”的博文

如何在twig中 call 一个保存在字符串变量中的function

attribute ¶ New in version 1.2:  The  attribute  function was added in Twig 1.2. attribute  can be used to access a "dynamic" attribute of a variable: 1 2 3 {{ attribute ( object , method ) }} {{ attribute ( object , method , arguments ) }} {{ attribute ( array , item ) }} 比如: {% for column in columns %} {% if column=='activity' %} {% for activity in company.activity %} {% endfor %} {% else if column='contacts' %} {% for contact in company.contacts %} {% endfor %} {% else %} {{ attribute(company, column) }} {% endif %} {% endfor %}

twig hint

symfony has introduced translation at here: http://symfony.com/doc/current/book/translation.html a point is if the transchoice is null, you will get an exception like: Unable to choose a translation So we need to check if the choice is valid or not before we use this twig tag. {{ ( chart . name ~ '.short' )| trans ({}, "charts" ) }} 如果你想合并string 的话,不要忘了在trans前面加个括号。 如果修改了trans 文件的名字,或者增加了一个tans 文件,需要clear cache, 即便是在dev中

vardump in twig

in a former post i give a solution to make var_dump in twig, the main idea is set twig into debug mode and debug a variable, but Symfony2 or such kind of framework make it impossible, the circle of reference make the dump infinitive. So we need to don in another way: First , we need to create a service which can be called by a twig file, the tutorial can see here : symfony2 offical service creatation for twig extension in your service.xml and then : /** * TVarDumper class. * * TVarDumper is intended to replace the buggy PHP function var_dump and print_r. * It can correctly identify the recursively referenced objects in a complex * object structure. It also has a recursive depth control to avoid indefinite * recursive display of some peculiar variables. * * TVarDumper can be used as follows, * * echo TVarDumper::dump($var); * * * @author Qiang Xue qiang.xue@gmail.com * @version $Id$ * @package System.Util * @since 3.0 * @modifi...

dump a variable

Twig: How to var_dump variables inside a template Do you sometimes wish you could just do a var_dump inside a Twig template while trying to debug some code? Well Twig comes with its own Debug extension to allow you to do just that. Firstly, add the following to your /app/config/config_dev.yml (you don’t want to do this on Prod): twig: debug: 1 services: debug.twig.extension: class: Twig_Extensions_Extension_Debug tags: [{ name: 'twig.extension' }] Now inside your Twig templates you can “var_dump” variables using the  built-in Twig Debug : {% debug nameOfVariable %}