本文由克里斯·佩里(Chris Perry)和里特什·庫馬爾(Ritesh Kumar)進行了同行評審。感謝SitePoint所有的同行評審員製作SitePoint內容的最佳狀態!
在本文中,我們將在JavaScript中概述模板。我們將首先討論什麼是JavaScript模板,何時應該使用它們以及如何實施它們,然後再詳細介紹一些流行的模板引擎。我們將專注於鬍子,車把和jQuery模板。
JavaScript模板
>提供了一種將HTML結構與內容分開的方法,從而提高了代碼庫的可維護性。它們對實時Web應用程序和國際化特別有益,通常需要使用相同格式顯示不同的內容。>
一些可以從JavaScript模板中受益的常見場景是實時的Web應用程序(例如,用於事件註釋的實時流式應用程序)或國際化(I18N),通常要求使用相同格式顯示不同的內容。 。我們如何實現JavaScript模板?
> Mustache.js
>關鍵點
9kb文件尺寸(小)
<span><span><span><script</span> id<span>="template"</span> type<span>="x-tmpl-mustache"</span>></span><span> </span></span><span><span> <span><p>Use the <strong>{{power}}</strong>, {{name}}!</p> </span></span></span><span><span></span><span><span></script</span>></span> </span>需要兩個參數:鬍子模板以及包含呈現模板所需的數據和代碼的視圖對象。在這種情況下,我們將用簡單的字符串替換名稱和功率變量,但是可以做更多的事情。例如,在數組上循環,或使用使用當前視圖作為視圖參數的特殊渲染函數。
//Grab the inline template var template = document.getElementById('template').innerHTML; //Parse it (optional, only necessary if template is to be used again) Mustache.parse(template); //Render the data into the template var rendered = Mustache.render(template, {name: "Luke", power: "force"}); //Overwrite the contents of #target with the rendered HTML document.getElementById('target').innerHTML = rendered;
> Mustache.js非常適合最少模板複雜性的小型項目和快速原型。要注意的一個關鍵是,我們可以用Mustache.js開始一個項目,然後輕鬆地升級到handlebars.js,因為模板(主要)相同。
如果您想閱讀更多有關小鬍子的信息,請查看:使用Mustache.js。創建HTML模板
handlebars.js建立在鬍子頂部,大多與鬍鬚模板兼容。簡而言之,它提供了所有Mustache.js提供的所有內容,並支持塊表達式和預編譯模板。預編譯的模板是一件大事,因為它們的性能提高了大幅度(在粗略的性能測試中,預編譯了handlebars.js模板在大約一半的鬍鬚模板時呈現)。塊表達式使您可以在模板中包含更多邏輯;這些最常見的例子之一是高級迭代器,例如。創建一個
<span><span><span><script</span> id<span>="template"</span> type<span>="x-tmpl-mustache"</span>></span><span> </span></span><span><span> <span><p>Use the <strong>{{power}}</strong>, {{name}}!</p> </span></span></span><span><span></span><span><span></script</span>></span> </span>
//Grab the inline template var template = document.getElementById('template').innerHTML; //Parse it (optional, only necessary if template is to be used again) Mustache.parse(template); //Render the data into the template var rendered = Mustache.render(template, {name: "Luke", power: "force"}); //Overwrite the contents of #target with the rendered HTML document.getElementById('target').innerHTML = rendered;>請參見codepen上的sitepoint(@sitepoint)的筆handlebars.js示例
> handlebars.js非常適合性能很重要的項目,我們並不擔心在頁面的重量中添加18kb。如果我們想利用塊表達式,這也是一個路要走。
>>請注意,要查看handlebars.js的性能優勢(以及大大減少的文件大小),我們必須使用預編譯模板。為了有效地這樣做,我們應該在我們的構建過程中添加handlebars.js模板編譯(Grunt對此具有很好的插件)。
>>如果您想了解有關車把的更多信息,請查看:
的初學者指南。> jQuery模板
>關鍵點
<span><span><span><script</span> id<span>="template"</span> type<span>="text/x-handlebars-template"</span>></span><span> </span></span><span><span> <span><p>Use the <strong>{{power}}</strong>, {{name}}!</p> </span></span></span><span><span></span><span><span></script</span>></span></span>請參閱codepen上的sitepoint(@sitepoint)的筆jQuery模板示例。
//Grab the inline template var template = document.getElementById('template').innerHTML; //Compile the template var compiled_template = Handlebars.compile(template); //Render the data into the template var rendered = compiled_template({name: "Luke", power: "force"}); //Overwrite the contents of #target with the renderer HTML document.getElementById('target').innerHTML = rendered;
jQuery模板非常適合已經包括jQuery的項目,因為文件大小很小。但是,如果您的項目不使用jQuery,則很難考慮到總文件大小。
當然還有許多其他解決此問題的解決方案,我們將在本文中詳細介紹。這是對其他一些流行選擇的快速概述;
>嵌入式JS模板(EJS)
那麼哪個是最好的?像大多數開發問題一樣,沒有銀彈。這取決於許多事情;
您已經在項目中使用了jQuery嗎?
JavaScript模板引擎之間的關鍵差異是什麼?
我可以在一個項目中使用多個JavaScript模板引擎嗎?
JavaScript中的功能可以更輕鬆地插值和多行字符串。儘管它們可用於生成HTML,但它們缺乏模板引擎提供的許多功能,例如支持可重複使用的組件和關注點的分離。但是,它們對於不需要成熟的模板引擎的簡單用例可能很有用。
> JavaScript模板引擎之間是否存在性能差異? 。一些發動機在編譯模板方面更快,而另一些引擎則在渲染方面表現出色。但是,對於大多數Web應用程序,這些差異不太可能對性能產生明顯的影響。
我如何通過JavaScript模板引擎來調試問題?
在模板開發中最常用時,它們也可以在其他情況下使用。例如,它們可用於生成電子郵件,PDF或任何其他基於文本的內容。模板引擎的關鍵優點是它們可以將數據與演示分開的能力,這在廣泛的應用程序中很有用。
以上是JavaScript模板引擎的概述的詳細內容。更多資訊請關注PHP中文網其他相關文章!