defining new variable within jquery template

前端 未结 3 1348
耶瑟儿~
耶瑟儿~ 2021-02-05 10:46

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 {{ v

相关标签:
3条回答
  • 2021-02-05 11:25

    I do:

    ${xxx=13, ""}
    

    and then i can use xxx in a sub or in the same template

    0 讨论(0)
  • 2021-02-05 11:28

    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/

    0 讨论(0)
  • 2021-02-05 11:40

    You can extend jquery template with eval tag

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

    and use

    {{eval var xxx = 123}}
    {{eval xxx += 23}}
    
    0 讨论(0)
提交回复
热议问题