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/