如何更改Handlebars.js的默认分隔符?

Geo*_* D. 16 handlebars.js laravel

我需要使用handlebars.js,我还使用Laravel的Blade模板引擎(PHP Framework).标签{{}}与刀片的占位符冲突完全相同.

如何将{{var}}更改为<%var%>?

ico*_*ast 28

虽然您可能无法配置Handlebars的表达式分隔符,但这不是解决Handlebars和Blade之间冲突的唯一方法.Blade提供的语法允许您通过指定应传递给Handlebars的内容来避免冲突.(这是合适的,因为Blade开始时会产生冲突,而且这是必要的,因为Blade在Handlebars看到它之前正在处理模板.)只是@在你想要Blade忽略的花括号前面并且按原样传递给Handlebars .这是一个更大的例子的非常简短的片段:

...
    <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>
...
Run Code Online (Sandbox Code Playgroud)

{{outlet}}将被传递给Handlebars,但{{ asset("css/bootstrap.theme.3.0.0.css") }}将由Blade处理.

  • 这应该是公认的答案,它不是一个技巧,而是一个适当的解决方案. (6认同)

jon*_*ert 8

我在 GitHub / npm 上创建了handlebars-delimiters以方便使用带有 Handlebars 的自定义 delims。

var Handlebars = require('handlebars');
var useDelims = require('handlebars-delimiters');

var a = Handlebars.compile('{{ name }}<%= name %>')({name: 'Jon'});
console.log(a);
//=> 'Jon<%= name %>'


// Pass your instance of Handlebars and define custom delimiters
useDelims(Handlebars, ['<%=', '%>']);
var b = Handlebars.compile('{{ name }}<%= name %>')({name: 'Jon'});
console.log(b);
//=> '{{ name }}Jon'
Run Code Online (Sandbox Code Playgroud)

compile 函数的想法来自/sf/answers/1342726311/

欢迎提出改进建议或请求请求!


bio*_*ndo 5

我做了这个功能。希望它可以对某人有用..

/**
* change the delimiter tags of Handlebars
* @author Francesco Delacqua
* @param string start a single character for the starting delimiter tag
* @param string end a single character for the ending delimiter tag
*/
Handlebars.setDelimiter = function(start,end){
    //save a reference to the original compile function
    if(!Handlebars.original_compile) Handlebars.original_compile = Handlebars.compile;

    Handlebars.compile = function(source){
        var s = "\\"+start,
            e = "\\"+end,
            RE = new RegExp('('+s+'{2,3})(.*?)('+e+'{2,3})','ig');

            replacedSource = source.replace(RE,function(match, startTags, text, endTags, offset, string){
                var startRE = new RegExp(s,'ig'), endRE = new RegExp(e,'ig');

                startTags = startTags.replace(startRE,'\{');
                endTags = endTags.replace(endRE,'\}');

                return startTags+text+endTags;
            });

        return Handlebars.original_compile(replacedSource);
    };
};

//EXAMPLE to change the delimiters to [:
Handlebars.setDelimiter('[',']');
Run Code Online (Sandbox Code Playgroud)


ajt*_*rds 5

您可以在没有.blade扩展名的情况下使用手柄模板创建文件,而不是更改分隔符.只需在刀片模板中包含这些文件即可.例如

刀片模板文件 - template.blade.php

@extends('master.template')

@section('content')

    @include('handlebars-templates/some-template-name') 

@stop
Run Code Online (Sandbox Code Playgroud)

some-template-name文件 - some-template-name.php

<script type="text/x-handlebars" data-template-name="content">
<div>
 <label>Name:</label>
 {{input type="text" value=name placeholder="Enter your name"}}
</div>
<div class="text">
<h1>My name is {{name}} and I want to learn Ember!</h1>
</div>
</script>
Run Code Online (Sandbox Code Playgroud)


Mat*_*all 4

这对于“标准”车把来说是不可能的。https://github.com/wycats/handlebars.js/issues/227

  • 当然,或者按照 GH 上的建议将模板完全存储在 HTML 之外:https://github.com/wycats/handlebars.js/issues/227#issuecomment-9950931 (2认同)