Javascript 下划线模板-部分(带RequireJS)
我正在使用Javascript 下划线模板-部分(带RequireJS),javascript,requirejs,templating,partials,underscore.js,Javascript,Requirejs,Templating,Partials,Underscore.js,我正在使用tplRequireJS插件,用于将我的模板导入并编译到我的应用程序中-类似于文本插件: define([ "tpl!../templates/newsfeed.html", "tpl!../templates/friends.html", "tpl!../templates/tag.html", "tpl!../templates/tags.html", ], function (renderNewsfeed, renderFriends, rende
tpl代码>RequireJS插件,用于将我的模板导入并编译到我的应用程序中-类似于文本代码>插件:
define([
"tpl!../templates/newsfeed.html",
"tpl!../templates/friends.html",
"tpl!../templates/tag.html",
"tpl!../templates/tags.html",
], function (renderNewsfeed, renderFriends, renderTag, renderTags) { … });
这一切都很好,但我已经到了一个理想的阶段,我想使用某种形式的partials
目前,如果我想在模板中使用模板,我必须将编译的部分传递给我正在渲染的模板,如下所示:
$('body').append(renderTags({
tags: tags,
renderTag: renderTag
}));
然后,在我的模板中:
<section class="tags-list">
<h1 class="h4 hidden">Tags</h1>
<% _.each(tags, function (tag) { %>
<%= renderTag({ tag: tag }) %>
<% }); %>
</section>
标签
如果我不将编译后的部分传递到模板上,那么它将找不到它
我的问题是,我怎样才能做得更好?如果我在RequireJS定义中定义为依赖项的模板对模板本身的可变范围(全局)可用,那么我可能不必将编译后的部分传递给模板
其次,拥有与RequireJS相同的依赖项定义是非常好的,但是对于模板:
define([
'tpl!../templates/tag.html'
], function (renderTag) {
// Obviously this can't be straight HTML, but you get the idea
<section class="tags-list">
<h1 class="h4 hidden">Tags</h1>
<% _.each(tags, function (tag) { %>
<%= renderTag({ tag: tag }) %>
<% }); %>
</section>
});
定义([
'tpl.../templates/tag.html'
],函数(renderTag){
//显然,这不可能是直接的HTML,但你明白了
标签
});
我可能在一个完全不同的星球上。如果我是,请有人解释一下他们是如何使用模板的。也许我需要切换模板引擎?我提出的解决方案是在模板内部实际使用require()
,以获取所需的部分,例如:
<%
require([
"tpl!../templates/partials/tags.html",
"tpl!../templates/partials/spotify-search.html",
"tpl!../templates/partials/popup.html"
], function (renderTags, renderSpotifySearch, renderPopup) { %>
// Template code goes here
// Partial example:
<%= renderTags({ tags: tags }); %>
<%
}); %>
//模板代码在这里
//部分示例:
将javascript添加到模板中会破坏视图的用途。相反,请使用一个可以实现这一点的框架,如CanJS: