问题描述
我在我的服务器端节点应用程序中使用Express + Handlebars,并且我想将客户端模板作为要呈现的页面的一部分发送到浏览器。
index.html
< html>
< head>
< title> {{title}}< / title>
< / head>
< body>
< div>
{{stuff}}
< / div>
< script id =more-templatetype =text / x-handlebars-template>
< div> {{more}}< / div>
< / script>
< / body>
不幸的是,句柄试图渲染#more-template脚本块中的东西。 (它只是删除 {{更多}}
,因为它在服务器模板的上下文中未定义。
有没有一种方法可以让它忽略script标签内的东西?(所以客户端模板可以使用它)
我已经看到了这个问题:,我宁愿只使用1个模板引擎。
所以我没有找到一种方法来忽略客户端模板很容易,但一般的共识是预先编译你的客户端模板。
$ b $ ol
npm install handlebars - g
handlebars client-template1.handlebars -f templates.js
< script src =templates.js>< / script>
var html = Handlebars.templates [client-template1](context);
额外信息
I'm using Express + Handlebars in my server side node application, and I want to send client templates to the browser as part of the page that I'm rendering.
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>
Unfortunately, handlebars tries to render the stuff in the #more-template script block. (Which just removes the {{more}}
because it's undefined in the server template's context.
Is there a way I can get it to ignore the stuff inside the script tag? (So that the client side template can use it)
I already saw this question: Node.js with Handlebars.js on server and client, I'd rather just use 1 template engine.
So I didn't find a way to ignore client templates easily, but the general consensus is to precompile your client templates.
- Install handlebars globally
npm install handlebars -g
- Precompile your templates
handlebars client-template1.handlebars -f templates.js
- Include templates.js
<script src="templates.js"></script>
- Execute the template
var html = Handlebars.templates["client-template1"](context);
Extra info
Using pre-compiled templates with Handlebars.js (jQuery Mobile environment)
http://handlebarsjs.com/precompilation.html
这篇关于有没有办法忽略Handlebars模板中的Handlebars模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!