这是 javascript 模板管理的理想方式吗?
为了管理我的模板(在 Mustache.js 中),我编写了一个库来提供帮助。例如,
<div id='beardTemplates'>
<div data-beard='tpl1'>
<h1>Hi, {{name}}</h1>
<!-- a normal comment -->
<!--@ Hi, {{name}}, this is an escaped template text. @-->
<div data-beard='subTpl1'>sub tpl1</div>
</div>
<div data-beard='subTpl2' data-beard-path='tpl1'>
sub tpl2
</div>
</div>
// compiling the templates
Beard.load()
// after calling the load() method, the templates will be wrapped into an
// object called Btpls, like :
Btpls =>
tpl1 =>
subTpl1
subTpl2
// calling the template function
Btpls.tpl1({name: 'Liangliang'})
// output:
<h1>Hi, Liangliang</h1>
<!-- a normal comment -->
Hi, Liangliang, this is an escaped template text.
// calling the nested sub functions
Btpls.tpl1.subTpl1()
// output:
sub tpl1
我不确定是否使用 来安全地转义模板文本?比如说,我只在 firefox、chrome 和 IE 下测试该库。它们都很好,但是其他浏览器(例如 Opera)是否还有其他潜在问题?
如果你想在其他浏览器上测试代码,可以从 https://github.com/jspopisno1/Beard 获取
In order to manage my templates (in mustache.js) I wrote a lib to help. For instance,
<div id='beardTemplates'>
<div data-beard='tpl1'>
<h1>Hi, {{name}}</h1>
<!-- a normal comment -->
<!--@ Hi, {{name}}, this is an escaped template text. @-->
<div data-beard='subTpl1'>sub tpl1</div>
</div>
<div data-beard='subTpl2' data-beard-path='tpl1'>
sub tpl2
</div>
</div>
// compiling the templates
Beard.load()
// after calling the load() method, the templates will be wrapped into an
// object called Btpls, like :
Btpls =>
tpl1 =>
subTpl1
subTpl2
// calling the template function
Btpls.tpl1({name: 'Liangliang'})
// output:
<h1>Hi, Liangliang</h1>
<!-- a normal comment -->
Hi, Liangliang, this is an escaped template text.
// calling the nested sub functions
Btpls.tpl1.subTpl1()
// output:
sub tpl1
The thing that I am not sure is that is using <!-- --> to escape template text a safe way? say, I only test the lib under firefox, chrome and IE. They are all fine, but is there any other potential problem with other browser, say, Opera?
If you want the code to test on other browser, you can get it from https://github.com/jspopisno1/Beard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经在 IE 7.0、8.0、Chrome、FireFox、Safari 和 Opera 中进行了测试。
只有 IE 会导致一些问题,因为它会压缩您尝试附加到 innerHTML 的 html 文本。它剥夺了主要评论。它需要更多的努力才能使其在 IE 中工作,但除此之外一切都很好。
你可以在这里看看 http://jspopisno1.github.com/Beard
I have tested in IE 7.0, 8.0, Chrome, FireFox, Safari and Opera.
Only IE will cause some problem, because it will compress the html text that you try to append to innerHTML. It strips the leading comments away. It requires some more effort to make it work in IE, but asides this is all fine.
You can have a look here http://jspopisno1.github.com/Beard