Use custom delimiters in the current Twig template

前端 未结 1 1756
萌比男神i
萌比男神i 2020-12-06 18:38

I use Twig to generate LaTeX documents. Twig\'s default delimiter syntax clashes badly with LaTeX\'s curly braces. Simply escaping LaTeX is no option as it makes the code co

相关标签:
1条回答
  • 2020-12-06 19:23

    As you can see, it's not recommanded to use this feature Customizing the Syntax

    BTW here's a quick and easy example to explain how to use custom delimiters 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');
    }
    
    0 讨论(0)
提交回复
热议问题