Для управления моими шаблонами (в 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
В чем я не уверен, так это в использовании <! - -> для безопасного удаления текста шаблона? скажем, я тестирую lib только под firefox, chrome и IE. Они все в порядке, но есть ли другие потенциальные проблемы с другим браузером, скажем, Opera?
Если вы хотите, чтобы код тестировался в другом браузере, вы можете получить его по адресу https://github.com/jspopisno1/Beard