在jQuery模板中定义新变量 [英] defining new variable within jquery template

查看:213
本文介绍了在jQuery模板中定义新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在jquery模板中定义一个新变量? 我阅读了官方的jquery模板文档,但在此上找不到任何内容. 我尝试了类似{{ var xxx=123 }}的方法,但没有成功. 最后,我通过

Is it possible to define a new variable within a jquery template? I read the official jquery template docs but could not find anything on this. I tried something like {{ var xxx=123 }} but it didn't work. Finally I am using a hack by doing

${$item.xxx=123,""}

,然后使用

$item.xxx

但是我确信这不是最好的方法...

but I am sure it is not the best approach...

推荐答案

我认为采用$ item方法并不算太糟糕.这与您寻找通过options参数传递到$ .tmpl的变量的位置一致.

I don't think that doing the $item approach is too bad. It is consistent with where you would look for variables that are passed in via the options parameter to $.tmpl.

基于小技巧,我使用了另一种方法此处,实际上是定义一个"var"模板标记.

Another approach that I have used, based on a small tip here, is to actually define a "var" template tag.

只需:

$.extend($.tmpl.tag, {
    "var": {
        open: "var $1;"
    }
});

然后您可以在模板中使用它,例如:

Then you can use it in your templates like:

{{var xxx=123}}
...
<div>${xxx}</div>

此外,有关自定义jquery模板标签的博客文章也不错:

Also, nice blog post here on custom jquery template tags: http://blog.sterkwebwerk.nl/2010/12/15/custom-jquery-template-tags-1/

这篇关于在jQuery模板中定义新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆