jQuery 模板中的条件
我有一个像这样的 jquery 模板设置:
<script id='tmpl-video_list' type='text/x-jquery-tmpl'>
<li>
<a href='${url}'><br/>
<span class='image_border'>
<span class='image_container'>
<img src='${thumb}' alt='' title='' />
</span>
</span>
</a>
<div class='search_block'>
<span class='name'>${caster_name}</span>
<a href='${url}'>
<span class='search_title'>${title}</span>
</a>
</div>
</li>
我发送的数据看起来像这样:
{
_index:i,
url: url,
thumb: thumb,
name: name,
title: title
}
所有这些都运行良好。我的问题是,是否有办法在那里放置条件。我知道 {{if}} 等文档中所述的内容,但我想知道你是否可以做这样的事情:
{{if ${_index}+1 % 5 == 0}} class='last_search_video' {{/if}}
我尝试过,但没有成功。实际上,我不喜欢对象中的 _index
,但我想我应该对此进行更改。我认为当前索引已传递到模板的循环中,但我不知道。
我还没有使用 jQuery 的模板插件,所以如果有人知道更好的插件,请随意推荐另一个。
I have a jquery template setup like this:
<script id='tmpl-video_list' type='text/x-jquery-tmpl'>
<li>
<a href='${url}'><br/>
<span class='image_border'>
<span class='image_container'>
<img src='${thumb}' alt='' title='' />
</span>
</span>
</a>
<div class='search_block'>
<span class='name'>${caster_name}</span>
<a href='${url}'>
<span class='search_title'>${title}</span>
</a>
</div>
</li>
And the data I'm sending looks something like this:
{
_index:i,
url: url,
thumb: thumb,
name: name,
title: title
}
All That is working great. My question is that if there is a way to put a conditional in there. I know about the {{if}} and such as stated in the docs but I'm wondering if you can do something like this:
{{if ${_index}+1 % 5 == 0}} class='last_search_video' {{/if}}
I tried that and it didn't work. I actually don't like the _index
in my object but I thought I'd give that a change. I'm thinking the current index is passed into the loop for the template but I have no idea.
I'm not married to jQuery's templating plugin so if someone knows a better plugin please feel free to suggest another one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好消息!你实际上并不需要它。将这个优秀的问题和答案适应您的问题,您需要做一些事情才能找到当前项目的索引您的渲染更容易:
将
options
参数中的函数添加到.tmpl()
,您可以使用它来检索当前项目的索引:修改您的模板以使用该功能:
这是一个简化的工作示例:http:// /jsfiddle.net/gsd6D/
Good news! You don't actually need it. Adapting this excellent question and answer to your problem, you need to do a few things to make finding the index of the current item you're rendering easier:
Add a function in the
options
parameter to.tmpl()
that you can use to retrieve the index of the current item:Modify your template to make use of that function:
Here's a simplified working sample: http://jsfiddle.net/gsd6D/
您不必在
{{if}}
中“模板化”_index
属性。你可以只写You don't have to "templatize" the
_index
property in the{{if}}
. You can just write