Use custom delimiters in the current Twig template.

I use Twig to create LaTeX documents. The default syntax for Twig strongly conflicts with LaTeX curly braces. LaTeX simple acceleration is not an option, as it makes the code completely unreadable. I know that I can define custom separators around the world , but I donโ€™t want to rewrite all my HTML templates to use the new syntax.

I also know about verbatim sections , but this makes the code really ugly:

\ihead{ {% endverbatim %} {{ title }} {% verbatim %} } 

Is there a way to change the syntax only for the current template or set of templates, for example:

 {% set_delimiters({ 'tag_comment' : ['<%#', '%>'], 'tag_block' : ['<%' , '%>'], 'tag_variable' : ['<%=', '%>'], 'interpolation': ['#<' , '>'] }) %} 
+7
php symfony twig
source share
1 answer

As you can see, he did not recommend using this function. Syntax tuning

Here's a quick and simple example to explain how to use custom separators in symfony:

service.yml

 services: templating_lexer: public: true parent: templating.engine.twig class: Acme\YourBundle\Twig\TwigLexerEngine 

TwigLexerEngine

 namespace Acme\YourBundle\Twig; use Symfony\Bundle\TwigBundle\TwigEngine; class TwigLexerEngine extends TwigEngine { public function setTwigLexer($lexer) { $this->environment->setLexer($lexer); return $this; } } 

Your controller

 public function yourAction() { $lexer = new \Twig_Lexer($this->get('twig'), array( 'tag_comment' => array('{*', '*}'), 'tag_block' => array('{', '}'), 'tag_variable' => array('{$', '}'), )); $templating = $this->get('templating_lexer'); $templating->setTwigLexer($lexer); return $templating->renderResponse('YourBundle::template.html.twig'); } 
+4
source share

All Articles