Conflict on Template of Twig and Vue.js

后端 未结 10 2123
一向
一向 2020-12-02 16:27

I\'m doing a program using Slim 2 that uses Twig as my templating engine. so It uses the syntax {{ foo }} in php file. On the other hand, I\'m using vue.js, it

相关标签:
10条回答
  • 2020-12-02 17:14

    Also, for those who don't want to change vue's delimiter, you can change Twig delimiter (using Silex php framework in this example):

    $app->before(function() use ($app){
        $app['twig']->setLexer( new Twig_Lexer($app['twig'], [
            'tag_comment'   => ['[#', '#]'],
            'tag_block'     => ['[%', '%]'],
            'tag_variable'  => ['[[', ']]'],
            'interpolation' => ['#[', ']'],
        ]));
    });
    

    https://twig.symfony.com/doc/2.x/recipes.html#customizing-the-syntax

    0 讨论(0)
  • 2020-12-02 17:14

    I'm using VueJs v2, with the syntax bellow:

    <img id="bookCover" style="border:none;width:200px" :src="book.cover">
    

    Where book.cover is one of the myVueInstance.$data.book fields.

    Here's the docs

    0 讨论(0)
  • 2020-12-02 17:15

    I read in another similar question to do:

    {{"{{vue.js variable here}}"}} 
    

    to make it shorter. It works in SOME cases for me. But, thought you might like to see it anyway...

    I didn't yet succeed to get it to work in all areas of my code.

    0 讨论(0)
  • 2020-12-02 17:23

    The best solution is not to change either ones delimiter.

    You can use the vuejs markup in twig like so

    {{ mytwigvar }} {{ '{{' }} myvuevar {{ '}}' }}
    

    This obviously is suboptimal, so redefine your twig loader to preprocess files and replace {{{ with {{ '{{' }} and }}} with {{ '}}' }} then you can write the markup as

    {{ mytwigvar }} {{{ myvuevar }}}
    

    Much nicer.

    0 讨论(0)
提交回复
热议问题