Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defining new variable within jquery template

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,""}

and later using

$item.xxx

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

like image 307
Darren Willis Avatar asked Feb 20 '11 11:02

Darren Willis


1 Answers

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.

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

Just do:

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

Then you can use it in your templates like:

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

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

like image 61
RP Niemeyer Avatar answered Nov 06 '22 00:11

RP Niemeyer