我如何加载javascript模板与他们的id

How can i load javascript templates with their ids?

本文关键字:他们的 id javascript 何加载 加载      更新时间:2023-09-26

一直在玩一些icanhazs .js模板,我定义在我的html页面的头部,像这样。

<script id="test" type="text/html">
<h1>Test</h1>
</script>

我想有我所有的iCanHaz模板在一个单一的文件,我不知道我应该如何导入它们。

提前感谢!

他们提供了一个如何从服务器上提取模板的例子,几乎在底部,如果你也使用jQuery:

$.getJSON('/myserver/templates.json', function (templates) {
    $.each(templates, function (template) {
        ich.addTemplate(template.name, template.template);
    });
});

当然,你必须有一些服务器端代码,以JSON形式提供模板,如何做到这一点取决于你使用的服务器端语言,正如Poelinca Dorin在他的评论中指出的。

像这样?(它适用于jsRender模板):

<!--#include virtual="Scripts/RenderTemplates.js" -->