artTemplate是騰訊開源的前端模板框架,和mustache,handlerbars類似,在web專案中可以很方便的使用,上手快,如果用過mustache,那麼幾乎可以快速切換到template框架上來。
學習過程:
1、語法介紹:
資料綁定:與angularjs類似,只不過視圖與模型是單向的綁定,模型改變,視圖改變,反過來則不行。
<script id="tpl1" type="text/template"> <h1 id="data-nbsp-mapping-nbsp-example">1、data mapping example</h1> <h2 id="message">{{message}}</h2> </script> //js中使用模板渲染 var data1 = {message:"hello,artTemplate is a javasript framework."}; $("node1").innerHTML = template("tpl1",data1);
條件判斷:這裡支援單一的if,也可以加入else分支,沒有else if分支。
{{if isShow}} <h3 id="满足条件展示消息-message">(2、满足条件展示消息:{{message}}</h3> {{else}} <h3 id="x-条件不满足-展示默认消息">(2x、条件不满足,展示默认消息</h3> {{/if}}
遍歷集合:
{{each list as item index}} <h3 id="the-nbsp-index-nbsp-of-nbsp-message-nbsp-is-nbsp-nbsp-index-nbsp-nbsp-item">the index of message is : {{index+1}} -> {{item}}</h3> {{/each}}
輔助函數:可以用來將後端請求的資料進行映射,例如1->正常,0->錯誤。使用的時候只需要在表達式後面通過"|func"的方式就可以,例如{{message | filterhandler}},其中filterhandler為自訂的輔助函數。
先定義一個輔助函數,這裡定義的是一個簡單的轉換日期格式函數。
template.helper("date2str",function(date){ var today = new Date(date); var year = today.getFullYear(); var month = today.getMonth()+1; if(month<10)month = "0"+month; var day = today.getDate(); if(day<10)day = "0"+day; return year+"-"+month+"-"+day; });
使用輔助函數
<p id="node4"></p> <script id="tpl4" type="text/template"> <h1 id="template-helper-nbsp-func-nbsp-example">4、template.helper func example</h1> <h3 id="today-nbsp-is-nbsp-datenow-nbsp-nbsp-date-str">today is {{datenow | date2str}}</h3> </script> //js代码中调用 var data4 = {datenow:new Date()}; $("node4").innerHTML = template("tpl4",data4);
預先編譯:與使用模板不同的是,預先編譯需要的是一個string類型的文檔片段,然後將資料交給預編譯後的模板進行渲染。
var tpl5 = "<h1 id="compile-nbsp-example">5、compile example</h1><h3 id="this-nbsp-is-nbsp-a-nbsp-string-nbsp-the-nbsp-type-nbsp-is-nbsp-not-nbsp-type">this is a string the type is not {{type}}</h3>"; $("node5").innerHTML = template.compile(tpl5)({type:"text/template"});
引用子範本:
<p id="node6"></p> <script id="tpl6" type="text/template"> <h1 id="include-nbsp-child-nbsp-template-nbsp-example">6、include child template example</h1> <p class="parenttemplate"> <h3 id="parent-nbsp-template">parent template</h3> {{include 'tpl6-child'}} </p> </script> <script id="tpl6-child" type="text/template"> <p class="childtemplate"> <h3 id="child-nbsp-template">child template</h3> </p> </script>
2、下載template.js庫,並引入到html檔案中。
3、這裡給一個綜合的例子,將前面介紹的一些文法練習一下:
<!doctype html> <html> <head> <meta charset="UTF-8"/> <title>artTemplate example</title> <style type="text/css"> *{margin:0;} h1,h2,h3{margin:3px;} h2,h3{text-indent:20px;} .parenttemplate{background:#ccc;width:600px;height:60px;} .childtemplate{background:lightblue;} </style> <script type="text/javascript" src="template.js"></script> <script> function $(id){return document.getElementById(id);} window.onload = function(){ //data mapping var data1 = {message:"hello,artTemplate is a javasript framework."}; $("node1").innerHTML = template("tpl1",data1); //if condition var data2 = {isShow:true,message:"hello,template"}; $("node2").innerHTML = template("tpl2",data2); data2.isShow = false; $("node2x").innerHTML = template("tpl2",data2); //list foreach var data3 = {list:["Javascript","JQuery","artTemplate"]}; $("node3").innerHTML = template("tpl3",data3); //helper function template.helper("date2str",function(date){ var today = new Date(date); var year = today.getFullYear(); var month = today.getMonth()+1; if(month<10)month = "0"+month; var day = today.getDate(); if(day<10)day = "0"+day; return year+"-"+month+"-"+day; }); var data4 = {datenow:new Date()}; $("node4").innerHTML = template("tpl4",data4); //compile example var tpl5 = "<h1 id="compile-nbsp-example">5、compile example</h1><h3>this is a string the type is not {{type}} </h3>"; $("node5").innerHTML = template.compile(tpl5)({type:"text/template"}); $("node6").innerHTML = template("tpl6",{}); //escape html $("node7").innerHTML = template("tpl7",{message:"<span>escape html tag</span>"}); } </script> </head> <body> <p id="node1"></p> <script id="tpl1" type="text/template"> <h1 id="data-nbsp-mapping-nbsp-example">1、data mapping example</h1> <h2 id="message">{{message}}</h2> </script> <p id="node2"></p> <p id="node2x"></p> <script id="tpl2" type="text/template"> <h1 id="if-nbsp-condition-nbsp-example">2、if condition example</h1> {{if isShow}} <h3 id="满足条件展示消息-message">(2、满足条件展示消息:{{message}}</h3> {{else}} <h3 id="x-条件不满足-展示默认消息">(2x、条件不满足,展示默认消息</h3> {{/if}} </script> <p id="node3"></p> <script id="tpl3" type="text/template"> <h1 id="list-nbsp-example">3、list example</h1> {{each list as item index}} <h3 id="the-nbsp-index-nbsp-of-nbsp-message-nbsp-is-nbsp-nbsp-index-nbsp-nbsp-item">the index of message is : {{index+1}} -> {{item}}</h3> {{/each}} </script> <p id="node4"></p> <script id="tpl4" type="text/template"> <h1 id="template-helper-nbsp-func-nbsp-example">4、template.helper func example</h1> <h3 id="today-nbsp-is-nbsp-datenow-nbsp-nbsp-date-str">today is {{datenow | date2str}}</h3> </script> <p id="node5"></p> <p id="node6"></p> <script id="tpl6" type="text/template"> <h1 id="include-nbsp-child-nbsp-template-nbsp-example">6、include child template example</h1> <p class="parenttemplate"> <h3 id="parent-nbsp-template">parent template</h3> {{include 'tpl6-child'}} </p> </script> <script id="tpl6-child" type="text/template"> <p class="childtemplate"> <h3 id="child-nbsp-template">child template</h3> </p> </script> <p id="node7"></p> <script id="tpl7" type="text/template"> <h1 id="escape-nbsp-html-nbsp-tag-nbsp-example">7、escape html tag example</h1> <h3 id="origin-nbsp-expression-nbsp-nbsp-message">origin expression : {{#message}}</h3> <h3 id="after-nbsp-escape-nbsp-nbsp-nbsp-message">after escape ==> : {{message}}</h3> </script> </body> </html>
執行這個範例,可以得到如下的效果:
以上是javascript前端模板引擎框架artTemplate使用總結 - CSDN博客的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。

是的,JavaScript的引擎核心是用C語言編寫的。 1)C語言提供了高效性能和底層控制,適合JavaScript引擎的開發。 2)以V8引擎為例,其核心用C 編寫,結合了C的效率和麵向對象特性。 3)JavaScript引擎的工作原理包括解析、編譯和執行,C語言在這些過程中發揮關鍵作用。

JavaScript是現代網站的核心,因為它增強了網頁的交互性和動態性。 1)它允許在不刷新頁面的情況下改變內容,2)通過DOMAPI操作網頁,3)支持複雜的交互效果如動畫和拖放,4)優化性能和最佳實踐提高用戶體驗。

C 和JavaScript通過WebAssembly實現互操作性。 1)C 代碼編譯成WebAssembly模塊,引入到JavaScript環境中,增強計算能力。 2)在遊戲開發中,C 處理物理引擎和圖形渲染,JavaScript負責遊戲邏輯和用戶界面。

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具