jsRender template embeddable in jquery plugin?

南笙酒味 提交于 2019-12-11 02:59:59

问题


Can you place a jsRender template within a .js file? The reason is, I want to contain my entire plugin, which I want to have use a template instead of writing all the code to dynamically create the nodes, in a single javascript file. Is this possible? If so how? If not, what are my options?


回答1:


If you create a template in a script block, you can get the compiled function, convert to string, and put it into your script file:

Run the code: var compiledFunctionString = $( "#myTemplate" ).template().toString();

Copy the string value of the resulting compiledFunctionString (which will be of the form "function anonymous($data, $view) { ...content... }") and put it into your script file as: function myTemplate($data, $view) { ...content... }.

Now you can use it as in:

var html = $.render( data, myTemplate );



回答2:


think about jQuery plugin: tmpl http://api.jquery.com/jquery.tmpl/



来源:https://stackoverflow.com/questions/8888998/jsrender-template-embeddable-in-jquery-plugin

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