【问题标题】:Handlebarsjs and put template into an external fileHandlebarsjs 并将模板放入外部文件
【发布时间】:2013-12-10 00:01:40
【问题描述】:

我有这个文件 test.html:

<script id="entry-template" type="text/x-handlebars-template">
template content
</script>

var source   = $("#entry-template").html();
var template = Handlebars.compile(source);

这样我可以正确看到我的模板,但是由于我有很多模板要实现,所以我想把这部分放在

<script id="entry-template" type="text/x-handlebars-template">
template content
</script>

到外部文件中。

我正在使用 phonegap,然后我无法使用服务器端语言。 Handlebarsjs 是在 phonegap 中实现模板的好东西吗? 如果我使用 jquery.load 加载模板(仅在需要时)可能会更好?

谢谢

【问题讨论】:

    标签: jquery templates


    【解决方案1】:

    我可以在这里为你想出两个答案。

    首先,您可以使用 ajax 从外部源加载文件。这个外部源可以是您的 Web 服务器,甚至可以是您的 Dropbox 的公共文件夹。这将让您在将来更新应用程序。外部源也可以是手机上的文件,由手机间隙加载。这是一个例子:loading handlebars.js template from external html file shows nothing

    第二种方法是您可以编写自定义 phonegap-native 处理程序并将原始 html 注入 javascript 函数。例如,第一个参数是文件名,第二个参数是整个 html 字符串。然后将该字符串加载为模板。

    【讨论】:

      【解决方案2】:

      是的,您可以将车把 js 与 Plugin-in 一起使用,并在单独的文件夹中管理您的模板文件。 由于浏览器不允许在浏览器中加载本地文件,因为它的 origin null 在mac中

      /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files
      

      在窗口中在快捷方式中添加参数--allow-file-access-from-files 然后打开

      【讨论】:

        猜你喜欢
        • 2013-10-12
        • 2011-12-21
        • 1970-01-01
        • 2021-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多