2011-10-17 50 views
0

爲了管理我的模板(在mustache.js中),我寫了一個lib來幫助。例如,這是一個JavaScript模板管理的理想方式嗎?

<div id='beardTemplates'> 
    <div data-beard='tpl1'> 
     <h1>Hi, {{name}}</h1> 
     <!-- a normal comment --> 
     <[email protected] 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,鉻和IE下的lib。它們都很好,但是其他瀏覽器還有其他任何潛在的問題,例如Opera嗎?

如果你想要的代碼來測試其他瀏覽器,你可以從https://github.com/jspopisno1/Beard

回答

0

我在IE 7.0,8.0,Chrome瀏覽器,火狐,Safari和Opera測試得到它。

只有IE會導致一些問題,因爲它會壓縮您嘗試附加到innerHTML的html文本。它將主要評論帶走。它需要更多的努力才能使它在IE中工作,但旁白,這一切都很好。

你可以看看這裏http://jspopisno1.github.com/Beard

相關問題