jsRender template embeddable in jquery plugin?

2019-07-15 17:17发布

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?

2条回答
Deceive 欺骗
2楼-- · 2019-07-15 17:40

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 );
查看更多
萌系小妹纸
3楼-- · 2019-07-15 17:51

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

查看更多
登录 后发表回答