jQuery Nested Templates using options parameter

天涯浪子 提交于 2019-12-11 07:02:52

问题


I don't understand how to reference the options parameter in nested templates.

Please see:

<div id="myContainer"></div>
<script type="text/javascript">
    var tmplMain = "<div><span>${title}</span>" + 
                   "<p style=\"border: solid 1px #f00\">${$item.details}</p>" +
                   "{{tmpl \"nestedTemplate\"}}</div>";

    var tmplNested = "<p style=\"border: solid 1px #0f0\">${$item.details}</p>";

    var _mainTemplate = jQuery.template("mainTemplate", tmplMain);
    jQuery.template("nestedTemplate", tmplNested);

    var _data = {title: "My Title"};
    var _options = {details: "My Details};

    jQuery.tmpl(_mainTemplate, _data, _options).appendTo("#myContainer");
</script>

Which will output this: http://i.stack.imgur.com/r7A7g.jpg

So either I'm not referencing "${$item.details}" correctly in the nested template or I'm not passing the options correctly in the {{ tmpl }} tag. I'm stumped.


回答1:


You would need to pass any options that you want to the {{tmpl}} tag. Something like:

{{tmpl($data, { details: $item.details}) "nestedTemplate" }}

You could even just pass $item for the options to the nested template, but $item has more than just your options on it.

Sample here: http://jsfiddle.net/rniemeyer/Xzgpr/



来源:https://stackoverflow.com/questions/5263036/jquery-nested-templates-using-options-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!