csvan
csvan

Reputation: 9474

Symfony2/Twig: can the default delimiters be changed?

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.

Upvotes: 2

Views: 1138

Answers (3)

Léo Benoist
Léo Benoist

Reputation: 2541

I suggest you this method :

<div ng-app="angularApp">
    {% verbatim %}
    {{ angular_code}}
    {% endverbatim %}
</div>

Upvotes: 2

Filo
Filo

Reputation: 2839

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

Upvotes: 1

trrrrrrm
trrrrrrm

Reputation: 11812

TwigBundle does not provide a configuration for the lexer delimiters as changing them would forbid you to use any templates provided by shared bundles (including the exception templates provided by TwigBundle itself).

However, you could use the raw tag around your angular templates to avoid the pain of escaping all curly braces: http://twig.sensiolabs.org/doc/tags/raw.html

https://groups.google.com/forum/#!msg/symfony2/kyebufz4M00/8VhF1KWsSAEJ

Upvotes: 0

Related Questions