在 Mustache 中,如何获取当前部分的索引
我正在使用 Mustache 并使用数据
{ "names": [ {"name":"John"}, {"name":"Mary"} ] }
我的胡子模板是:
{{#names}}
{{name}}
{{/names}}
我想要做的是获得数组中当前数字的索引。比如:
{{#names}}
{{name}} is {{index}}
{{/names}}
并打印出来
John is 1
Mary is 2
可以用 Mustache 得到这个吗?或者使用车把或其他扩展?
I am using Mustache and using the data
{ "names": [ {"name":"John"}, {"name":"Mary"} ] }
My mustache template is:
{{#names}}
{{name}}
{{/names}}
What I want to be able to do is to get an index of the current number in the array. Something like:
{{#names}}
{{name}} is {{index}}
{{/names}}
and have it print out
John is 1
Mary is 2
Is it possible to get this with Mustache? or with Handlebars or another extension?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
这就是我在 JavaScript 中的做法:
你的模板:
This is how I do it in JavaScript:
Your template:
作为参考,此功能现已内置于 Handlebars 中,并且与 Mustache 兼容。
使用
{{@index}}
约翰是 0
玛丽是 1
For reference, this functionality is now built in to Handlebars which has compatibility with Mustache.
Use
{{@index}}
John is 0
Mary is 1
在handlebars.js 中,您可以使用辅助函数来完成此操作。 (事实上,这里提到的车把的优点之一 http://yehudakatz.com/2010/09 /09/announcing-handlebars-js/ 是你可以使用助手,而不必在调用模板之前重写对象
因此,你可以这样做:
然后你的模板可以是这样的:
你的数据是。与之前相同,即:
您会得到以下输出:
为了使其真正起作用,每次渲染模板时都需要重置 nameIndex,因此您可以在列表的开头有一个重置助手。代码如下所示:(
这可以正确渲染模板两次。)
In handlebars.js you can accomplish this with a helper function. (In fact, one of the advantages mentioned about handlebars here http://yehudakatz.com/2010/09/09/announcing-handlebars-js/ is that you can use helpers instead of having to rewrite the objects before calling the template.
So, you could do this:
And, then your template can be this:
Your data is the same as before, i.e.:
And you get this output:
To make this really work, nameIndex needs to get reset each time the template is rendered, so to do that you can have a reset helper at the beginning of the list. So full code looks like this:
(This can correctly render the template twice.)
您可以在对象列表上运行以下循环。
该方案具有以下优点:
代码:
说明:
使用函数代替变量名,因此数据没有发生变异。闭包用于返回在循环中创建函数时的“i”值,而不是循环结束时的 i 值。
You can run the following loop on your list of objects.
This solution has the following advantages:
Code:
Explanation:
A function is used instead of a variable name, so the data is not mutated. Closure is used to return the value of 'i' at the time the function is created in the loop instead of the value of i at the end of the loop.
对于 Mustache 来说,更好的方法是使用一个使用
indexOf
获取索引的函数:在模板中:
缺点是这个
index
函数对数组进行了硬编码data.names
为了使其更加动态,我们可以使用另一个函数,如下所示:在模板中使用:
还有一个小缺点是您必须将数组名称传递给本示例中的索引函数
名称
,{{#index}}名称{{/index}}
。A better approach for Mustache would be using a function that gets the index using
indexOf
:In your template:
The drawback is that this
index
function has the array hard codeddata.names
to make it a more dynamic, we can use another function like this:Usage in your template:
Also a small drawback is that you have to pass the array name to the index function in this example
names
,{{#index}}names{{/index}}
.这里的好帮手:
来源:https://gist.github.com/1048968
Great helper here:
Source: https://gist.github.com/1048968
推荐的解决方案
我的建议是添加
index
键建议的 Mustache Index 解决方案存在问题
其他建议的解决方案并不那么干净,如下详述...
@Index
< /strong>Mustache 不支持
@Index
IndexOf
此解决方案运行时间为 O(n^2),因此它没有最佳表现。此外,还必须为每个列表手动创建索引。
全局变量
这个解决方案运行时间为 O(n),因此这里的性能更好,但也“污染了全局空间”(即向
window
对象添加属性,并且在浏览器窗口关闭之前,垃圾收集器不会释放内存),并且必须为每个列表手动创建索引。局部变量
该解决方案运行时间为 O(n),不会“污染全局空间”,并且不需要为每个列表手动创建。然而,该解决方案很复杂,并且不能很好地处理嵌套列表。
Recommended Solution
My recommendation is to add an
index
keyProblems with proposed Mustache Index solutions
Other proposed solutions are not as clean, as detailed below...
@Index
Mustache does not support
@Index
IndexOf
This solution runs in O(n^2) time, and therefore it does not have the best performance. Also the index must be manually created for each list.
Global Variable
This solution runs in O(n) time, so better performance here, but also "pollutes the global space" (i.e. adds properties to the
window
object, and the memory is not freed by the garbage collector until the browser window closes), and the index must be manually created for each list.Local Variable
This solution runs in O(n) time, does not "pollute the global space", and does not need to be manually created for each list. However, the solution is complex and does not work well with nested lists.
如果您可以控制 JSON 字符串的输出,那么请尝试一下。
因此,当您创建 JSON 字符串时,请将索引添加为每个对象的另一个属性。
If you can control the output of the JSON string, then try this out.
So when you make your JSON string, add the index as another property for each object.
只要您不访问多个数组,您就可以将索引存储在助手上,并在上下文发生变化时递增。我正在使用类似于以下内容的东西:
As long as you're not traveling into more than one array you can store the index on the helper and just increment when the context changes. I'm using something similar to the following:
(在节点 4.4.7、mustache 2.2.1 中测试。)
如果您想要一种干净的功能方式来完成此操作,不涉及全局变量或改变对象本身,请使用此函数;
当你组装视图时使用它;
这种方法的巧妙之处在于它创建了一组新的“视图模型”对象,使用旧的集合作为原型。您可以像读取
person.firstName
一样读取person.id
。但是,此函数根本不会更改您的人员对象,因此其他代码(可能依赖于不存在的 ID 属性)不会受到影响。算法是 O(N),非常好而且快。
(Tested in node 4.4.7, moustache 2.2.1.)
If you want a nice clean functional way to do it, that doesn't involve global variables or mutating the objects themselves, use this function;
Use it when you're assembling your view;
The neat thing about this approach is that it creates a new set of 'viewmodel' objects, using the old set as prototypes. You can read the
person.id
just as you would readperson.firstName
. However, this function doesn't change your people objects at all, so other code (which might have relied on the ID property not being there) will not be affected.Algorithm is O(N), so nice and fast.
我正在使用预计算函数将“@index”注入数组。
然后像这样使用:
I'm using a pre calculate function to inject '@index' into array.
and then using like this:
我的主要用例是能够在项目上放置“活动”类。我尝试将“equals”帮助器与“index_for_each”帮助器结合起来,但它太复杂了。
相反,我想出了以下基本的“主动”助手。它非常具体,但对于任何菜单/选择列表场景来说都是一个常见的用例:
用法:
将使第三个菜单项具有“class='active'”。
助手在这里(这是 CoffeeScript 版本):
My major use case for this was the ability to place an 'active' class on items. I messed around with combining an "equals" helper with and "index_for_each" helper, but it was way too complicated.
Instead, I came up with the following basic "active" helper. It's very specific, but is such a common use case for any menu / select list scenario:
Usage:
Would make the 3rd menu item have a "class='active'".
The helper is here (this is the CoffeeScript version):
这工作得很好。
This works perfectly fine.