有没有办法忽略Handlebars模板内的Handlebars模板?

我在我的服务器端节点应用程序中使用Express + Handlebars,我想将客户端模板作为我要呈现的页面的一部分发送到浏览器。

的index.html

<html> <head> <title>{{title}}</title> </head> <body> <div> {{stuff}} </div> <script id="more-template" type="text/x-handlebars-template"> <div>{{more}}</div> </script> </body> 

不幸的是,句柄试图渲染#more-template脚本块中的东西。 (这只是删除{{more}}因为它在服务器模板的上下文中是未定义的。

有没有办法让我忽略脚本标签内的东西? (所以客户端模板可以使用它)

我已经看到了这个问题: 服务器和客户端上的Handlebars.js的Node.js ,我宁愿只使用1模板引擎。

所以我没有find方法轻易地忽略客户端模板,但一般的共识是预编译客户端模板。

  1. 安装全局的npm install handlebars -g
  2. 预编译你的模板handlebars client-template1.handlebars -f templates.js
  3. 包含templates.js <script src="templates.js"></script>
  4. 执行模板var html = Handlebars.templates["client-template1"](context);

额外的信息
使用Handlebars.js(jQuery Mobile环境)的预编译模板
http://handlebarsjs.com/precompilation.html

一般情况下,当你的模板被编译了两次(例如,当你只是服务器端)并且你想在第一次运行时忽略模板标签的时候,你可以通过添加一个反斜杠来简单地告诉句柄忽略它们:

 {{variable1}} \{{variable2}} 

当编译variable1设为value1variable2未定义时,您将得到:

 value1 {{variable2}} 

(而不是使用空stringreplace第二个标签)。

编译第二次运行时,将variable2设置为value2 ,您将得到:

 value1 value2 

不是完美的解决scheme(这将是一个设置让把手留下未定义的variables),但为我的特殊情况工作。