使用RequireJS加载HTML模板

时间:2014-01-30 14:09:06

标签: html requirejs

我正在尝试使用RequireJS加载HTML模板,我正在尝试使用RequireJS插件。 https://github.com/ZeeAgency/requirejs-tpl。以下是插件中提供的用于加载模板的示例代码。

define(['tpl!your-template-path.tpl'], function(tpl) {
    return tpl({your: 'data'});
});

此函数的问题在于无法将数据传递给此函数并获取呈现的html。我是javascript的新手,所以请帮忙。

您是否知道使用requirejs加载HTML模板的更简单方法?

1 个答案:

答案 0 :(得分:3)

我使用text插件,underscore用于模板

你这样使用它:

your_template.html

<p>hello <%=name%><p>

在您的requirejs文件中,

define(['underscore', 'text!./your_template.html'], function(_, yourTemplate ) {
    var compiledTemplate = _.template(yourTemplate );
    return = compiledTemplate({name: 'moe'});
});