Symfony2中/枝:可以将默认的分隔符可以改变? [英] Symfony2/Twig: can the default delimiters be changed?

查看:113
本文介绍了Symfony2中/枝:可以将默认的分隔符可以改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在嫩枝,用于评估前pressions默认的分隔符是{{...}}。有什么办法这是可以改变别的东西,如{[{...}]}?

问,因为我需要通过服务嫩枝AngularJS泛音和AngularJS使用数据绑定完全相同的分隔符。我知道如何改变分隔符在AngularJS,但由于有数百那些和少得多的人枝杈,它会运行得更好,以改变那些枝杈代替。


解决方案

  

嫩枝允许块分隔符语法的一些定制。它的
  不推荐使用此功能为模板,将被捆绑
  您的自定义的语法。但对于具体项目,它可以使意义
  更改缺省值。


  
  

要更改块分隔符,你需要创建自己的词法分析器
  对象:


  $树枝=新Twig_Environment();$词法分析器=新Twig_Lexer($树枝,阵列(
    tag_comment'=>阵列('{#,#}'),
    tag_block'=>阵列('{%','%}'),
    tag_variable'=>阵列('{{,}}'),
    '插'=>阵列('#{,}),
));
$ twig-> setLexer($词法分析器);

文档: http://twig.sensiolabs.org/doc/ recipes.html#定制的非语法

In Twig, the default delimiters for evaluating expressions is {{ ... }}. Is there any way this can be changed to something else, such as {[{ ... }]}?

Asking since I need to serve AngularJS partials via Twig, and AngularJS uses the exact same delimiters for data binding. I know how to change the delimiters in AngularJS, but as there are hundreds of those and much fewer Twig ones, it would work much better to change the Twig ones instead.

解决方案

Twig allows some syntax customization for the block delimiters. It's not recommended to use this feature as templates will be tied with your custom syntax. But for specific projects, it can make sense to change the defaults.

To change the block delimiters, you need to create your own lexer object:

$twig = new Twig_Environment();

$lexer = new Twig_Lexer($twig, array(
    'tag_comment'   => array('{#', '#}'),
    'tag_block'     => array('{%', '%}'),
    'tag_variable'  => array('{{', '}}'),
    'interpolation' => array('#{', '}'),
));
$twig->setLexer($lexer);

Documentation: http://twig.sensiolabs.org/doc/recipes.html#customizing-the-syntax

这篇关于Symfony2中/枝:可以将默认的分隔符可以改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆