- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要捆绑客户端模板供其他人用作 Backbone 组件库的一部分。我不能使用 RequireJS 或任何其他 AMD 解决方案。
我的想法是将所有 HTML 模板合并到一个 ingle JS 文件中,该文件定义了包含模板的变量。然后有人只需要做:
<script type="text/javascript" src="/js/templates.js"></script>
templates.js 看起来像
var ns = ns || {};
ns.templates = {};
ns.templates['my-special-list'] = "<% _.each(stuff, function(model) { %><li><% print(model.get('title')); %></li><% }); %>";
然后我的观点可以做这样的事情:
var V = Backbone.View.extend({
initialize: function() {
if (_.isUndefined(this.template)) {
this.template = _.template(ns.templates['my-special-list']);
} else {
this.template = _.template(this.template);
}
}
render: function() {
this.$el.html(this.template.render(this.options));
}
}
这个想法似乎可行。仍然允许人们毫不费力地传递他们自己的模板,同时仍然允许我在构建时将我们所有的模板组合到一个 HTML 文件中。
尽管如此,我还是感觉到了所有这些的复杂性。对于初学者,每个新行都需要转换为\n、转义字符等。
老实说,我想不出另一种方法。我试着用谷歌搜索,但没有看到太多帮助。 RequireJS 只是提供了一种加载文本的好方法,但这对我没有太大帮助。
是否有更好的方法来完成我想要的,或者我的方法是否已经很好了?
最佳答案
你熟悉Grunt吗? ?在我的一个项目中,我使用 JST task在构建时将我的个人模板编译成一个文件。我将它们分别存储为单独的 HTML 文件,然后将其保存在 Gruntfile.js 中:
jst: {
compile: {
options: {
namespace: 'app.templates',
processName: function(filename) {
// simplify the template names
filename = filename.replace('app/templates/', '');
return filename.replace('.html', '');
}
},
files: {
"<%= yeoman.app %>/scripts/templates.js": ["<%= yeoman.app %>/templates/{,*/}*.html", "<%= yeoman.app %>/templates/**/{,*/}*.html"]
}
}
}
例如,我的标题模板 (app/templates/inc/header.html) 如下所示:
<h1 class='topbar-header'><%= title %></h1> <h2 class="subtitle"><%= subtitle %></h2>
它由 JST 编译并通过 app.templates['inc/header']
提供,这实际上是一个您使用包含参数的对象调用的函数(不是字符串)。对于我的标题模板,我必须传入一个具有 title
和 subtitle
属性的对象。
var template = app.templates['inc/header'];
var code = template({title: 'Hello', subtitle: 'World'});
this.$el.html(code);
关于javascript - 分发基于 underscore.js 的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17217254/
如何检查underscore.js中数组中的元素是否存在?例如,我有['aaa', 'bbb', 'cfp', 'ddd'],并想检查'cfp'是否存在。如果可以,我想显示一些文本。我的以下代码无法正
我有一个 array = [1,2,3,4,5]并想使用 underscore.js 获得累积和数组. 我想要的结果是: [1,3,6,10,15] 我想要数组而不是累积总和 15 作为值。任何帮助,
我正在使用 underscore.js的模板库,我不确定如何在模板中使用逻辑。例如,我想在模板中打印一组标签。最好的方法是什么? Javascript: bunny_data = { name:
我正在尝试将我的对象数组格式化为一个数组以供组件使用。我需要将对象的确切值映射到正确的位置。 例如 我的原始数据: var rawData = [ { name: 'j
我有一个带有键的对象 var obj = { a: { fruit: 'Apple' }, b: { fruit: 'Banana' } } 我想快速将键 (a/b) 移动到作为属性 name 的值
我有一个像这样的 json 数组: myArray=[{ a:1, b:[{c:"x",d:"y"}, {c:"r", d:"s"}...] },
可以说我有这个对象数组。 var initialData = [{Title:"Hat",Price:49.95}, {Title:"Pants",Price:7
在使用 _.findWhere 函数时,我无法弄清楚如何在 underscore.js 中动态设置属性。 这是该函数的文档: findWhere_.findWhere(list, properties
我正在将一个相当大的 php 模板(其中包含基本逻辑的页面)转换为一个 underscore.js 模板。 问题是我一直有错误,并且它是缩小的“编译”版本在抛出错误时没有提供有用的信息或行号。 有没有
在 PHP 中,特别是在 Wordpress 中,__('string') 和 _x('string') 有什么区别? 我正在阅读 Wordpress 文档并感到困惑。以下混淆的好例子来自 Wordp
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭3年前。 社区在上个月审核了是
如何在 underscore.js 中查询日期?我知道我不能做“之间”子句,但请参阅下面的代码作为示例。我的目标是根据用户指定的日期范围将 JSON 数据加载到图表中。 var testdata=[{
$.each(asList(1, 2, 3), new Block() { public void apply(Integer item) { System.out.print
我遇到了在线文档与我在程序中看到的在 GO 代码中访问 C 结构的行为之间的脱节。 go version 说我正在使用: go version go1.4.2 linux/amd64 根据GO CGO
我正在浏览Underscore.js api我注意到 _.escape逃脱& , , " , ' , 和 /字符。让我吃惊的是逃跑/ . 有没有逃避的理由/我不知道的字符? 最佳答案 编辑 : 好吧
我想发送带有几个下划线的字符串 "__hello__its_me_"但 Discord 将其解释为 Markdown 并在我的字符串下划线或斜体。 我知道我必须用反斜杠转义下划线,但在 Visual
我有一个连接到 Visual Studio Online 的 Azure 网站。连接两者时,Visual Studio Online 会在托管构建 Controller 上创建一个持续交付构建运行。此
当我尝试呈现以下模板时,在运行时出现编译错误: Title: Author:
我发现自己经常使用以下模式 var line = "12|John Doe" var pieces = line.split("|") var user = { id : pieces[0],
我正在尝试在主干中使用带下划线的 Handlebars 样式模板(与require.js一起使用)。 我有以下几点: _.templateSettings.interpolate = /\{\{(.+
我是一名优秀的程序员,十分优秀!