这是 javascript 模板管理的理想方式吗?

发布于 2024-12-10 10:30:05 字数 1210 浏览 5 评论 0原文

为了管理我的模板(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2024-12-17 10:30:05

我已经在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文