How to change the default delimiter of Handlebars.js?

前端 未结 8 1800
半阙折子戏
半阙折子戏 2020-12-10 02:41

I need to use handlebars.js and I also use Blade template engine from Laravel (PHP Framework). The tags {{}} conflict with blade\'s placeholders that are exactly the same.

相关标签:
8条回答
  • 2020-12-10 02:57

    There is an option to tell template engine for not parsing certain part of code and treat it as plain text.

    Find the following ways to do it, hope it helps somebody.

    In blade template engine (laravel) you can use @verbatim Directive. So that you dont have to add @ to every variable. Example :

    @verbatim
        <div class="container">
            Hello, {{ name }}.
        </div>
    @endverbatim
    

    Similarly for twig template engine (symfony) you can block the whole code by using

    {% verbatim %}
        <div>
            My name is {{name}}. I am a {{occupation}}.
        </div>
    {% endverbatim %} 
    
    0 讨论(0)
  • 2020-12-10 03:07

    Although it may be true that you can't configure Handlebars' expression delimiters, that's not the only way to resolve the conflict between Handlebars and Blade. Blade provides syntax that allows you to avoid the conflict by designating what should be passed on to Handlebars. (This is fitting, since Blade created the conflict to begin with, and it's necessary, since Blade is processing the template before Handlebars ever sees it.) Just prepend @ before the curly braces that you want Blade to ignore and pass as-is to Handlebars. Here's a very short snippet of a much larger example:

    ...
        <link
            rel="stylesheet"
            type="text/css"
            href="{{ asset("css/bootstrap.theme.3.0.0.css") }}"
        />
        <title>Laravel 4 Chat</title>
    </head>
    <body>
        <script type="text/x-handlebars">
            @{{outlet}}
        </script>
    ...
    

    {{outlet}} will be passed to Handlebars, but {{ asset("css/bootstrap.theme.3.0.0.css") }} will be handled by Blade.

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