何时使用JavaScript模板引擎? [英] When to use JavaScript template engines?

查看:123
本文介绍了何时使用JavaScript模板引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是来自Ben Nadel演示单页长效AJAX应用程序的JavaScript模板示例: [source]

 < script id =contact-list-item-template类型= 应​​用/模板 > 

< li class =contact clear-fix>

< div class =summary>
< a class =name> $ {name}< / a>
< / div>

< div class =actions>
< a href =javascript:void(0)class =more> more< / a> &安培; NBSP; |&安培; NBSP;
< a href =#/ contacts / edit / $ {id}class =edit>编辑< / a> &安培; NBSP; |&安培; NBSP;
< a href =#/ contacts / delete / $ {id}class =delete>删除< / a>
< / div>

< dl class =details clear-fix>
< dt>
名称:
< / dt>
< dd>
$ {name}
< / dd>
< dt>
手机:
< / dt>
< dd>
$ {phone}
< / dd>
< dt>
电子邮件:
< / dt>
< dd>
$ {email}
< / dd>
< / dl>

< / li>



我想问什么是使用JavaScript模板引擎的目的是什么?
是否用于节省带宽?这只是分离关注点的问题吗?它会帮助对抗浏览器内存泄漏问题吗?



什么时候应该使用模板引擎,何时使用原始HTML AJAX响应更容易?



相关讨论:

JQuery模板引擎

解决方案

在几种情况下,模板是一个很好的解决方案:
$ b


  • 从服务器加载所有数据,特别是在富列表显示中
  • 添加或更新新项目在列表中

  • 任何需要向页面添加新复杂内容的地方

  • 需要客户端HTML呈现的任何内容



资料来源: http: //www.west-wind.com/Weblog/posts/509108.aspx


Here is an example of JavaScript template from Ben Nadel's demo single page long-lived AJAX application taken from: [source]

<script id="contact-list-item-template" type="application/template">

    <li class="contact clear-fix">

            <div class="summary">
                    <a class="name">${name}</a>
            </div>

            <div class="actions">
                    <a href="javascript:void( 0 )" class="more">more</a> &nbsp;|&nbsp;
                    <a href="#/contacts/edit/${id}" class="edit">edit</a> &nbsp;|&nbsp;
                    <a href="#/contacts/delete/${id}" class="delete">delete</a>
            </div>

            <dl class="details clear-fix">
                    <dt>
                            name:
                    </dt>
                    <dd>
                            ${name}
                    </dd>
                    <dt>
                            phone:
                    </dt>
                    <dd>
                            ${phone}
                    </dd>
                    <dt>
                            email:
                    </dt>
                    <dd>
                            ${email}
                    </dd>
            </dl>

    </li>

I want to ask what is the purpose of using a JavaScript template engines like that? Is it for save of the bandwidth? Is it just a matter of Separation of concerns? Will it help in fighting the browser memory leaks problems?

When should I use template engine and when it is easier to go with raw HTML AJAX responses?

Related discussion:

JQuery templating engines

解决方案

Templating is a good solution in a few scenarios:

  • Loading all data from the server especially in rich list displays
  • Adding or updating new items in lists
  • Anywhere you need to add new complex content to the page
  • Anything that requires client side HTML rendering

Source : http://www.west-wind.com/Weblog/posts/509108.aspx

这篇关于何时使用JavaScript模板引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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