jsRender模板嵌入jquery插件? [英] jsRender template embeddable in jquery plugin?

查看:102
本文介绍了jsRender模板嵌入jquery插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在.js文件中放置jsRender模板吗?原因是,我想要包含我的整个插件,我想在一个javascript文件中使用模板而不是编写所有代码来动态创建节点。这可能吗?如果是这样的话?如果没有,我的选择是什么?

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?

推荐答案

如果在脚本块中创建模板,则可以获得已编译的函数,转换为字符串,并将其放入脚本文件中:

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:

运行代码: var compiledFunctionString = $(#myTemplate)。template( ).toString();

复制生成的 compiledFunctionString 的字符串值(其形式为函数匿名($ data,$ view){... content ...})并将其放入脚本文件中: function myTemplate($ data,$ view){... content ...}

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 );

这篇关于jsRender模板嵌入jquery插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆