有没有办法忽略Handlebars模板中的Handlebars模板? [英] Is there a way to ignore Handlebars templates within a Handlebars template?

查看:100
本文介绍了有没有办法忽略Handlebars模板中的Handlebars模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的服务器端节点应用程序中使用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标签内的东西?(所以客户端模板可以使用它)



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

解决方案

所以我没有找到一种方法来忽略客户端模板很容易,但一般的共识是预先编译你的客户端模板。
$ b $ ol

  • 全局安装句柄 npm install handlebars - g

  • 预编译您的模板 handlebars client-template1.handlebars -f templates.js

  • 包含templates.js < script src =templates.js>< / script>

  • 防爆执行模板 var html = Handlebars.templates [client-template1](context);

  • 额外信息

    与Handlebars.js(jQuery Mobile环境)一起使用预编译的模板

    http://handlebarsjs.com/precompilation.html


    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.

    1. Install handlebars globally npm install handlebars -g
    2. Precompile your templates handlebars client-template1.handlebars -f templates.js
    3. Include templates.js <script src="templates.js"></script>
    4. 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模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆