一、背景
有兩個大項目是從RD那邊遷移過來,因為項目初期FE無人力跟進,所以都是後端同學直接用Smarty完成的前端部分;所以考慮到遷移的成本和方案,索性升級一下當前的JS模板引擎。
二、大致方案
支援extends標籤
支援block標籤
1、標籤文法
為了便於模板詞法分析,在模板左定界符後加上@來標識,標籤名替換為屬性設定方式,如extends標籤:
<%@ extends="layout/layout.html" %>
2、extends實現方案
將整個extends標籤替換成其指定的父模板的內容
3、block實現方案
首先理解block的概念:在父模板layout.html中提前挖好的坑,子模板中對block的實現,就好比蘿蔔,最後渲染的時候,就是一個蘿蔔一個坑的填充。
這樣一來事情就好辦多了,可將block的定義編譯成js function的調用,假設父模板中block的定義是:
<%@ block="block_header" %>
最終可將其編譯成:
block_header();
4、block進階:父模板支援預設內容
smarty中的block是支援預設內容的,比如:(為了將smarty和js模板引擎區分開,下面用<&和&>分別標識smarty的定界符)
<&block name="block_header" &>
<div class="x-header">
...
</div>
<&/block&>
那麼,我們可以對應的在js模板引擎中支援如下格式:
<% block="block_header" {%>
<div class="x-header">
...
</div>
<%}%>
沒錯,用一個相對投機的{和}將block預設內容包圍起來,編譯時間,將block_header編譯成一個function即可,為了避免和子模板中的block實現(也是一個function)命名衝突,所以給父模板中的block name定義加上parent標識,如:
function block_header__parent_ (){
__html += '<div class="x-header">\n';
__html += '\t...\n';
__html += '</div\n>';
}
這樣,如果子模板中沒有對該block的(function)實現,那麼這裡可以直接執行這個blockx__parent方法即可。最終的樣子差不多是這樣的:
block_header__parent_();
function block_header__parent_ (){
__html += '<div class="x-header">\n';
__html += '\t...\n';
__html += '</div\n>';
}
5、block進階:子模板有block實現,甚至設定prepend、append模式
如題,假設子模板中有對該block的實現,可以用function來解決,如:
<%function block_header(){%>
我是header
<%}%>
那麼,按照smarty的理解,應該是要用子模板中的block來覆蓋父模板的block的,所以父模板block的定義處可編譯為:
block_header();
function block_header__parent_ (){
__html += '<div class="x-header">\n';
__html += '\t...\n';
__html += '</div\n>';
}
而對於smarty中定義的prepend模式繼承block,如:
<&block name="block_header" prepend &>
我是header
<&/block&>
我們可以這樣來簡單類比:
<%function block_header(prepend){%>
我是header
<%}%>
對,通過給block_header添加prepend參數的方式來解決。對於這種形式,父母般block的定義處可編譯為:
block_header();
block_header__parent_();
function block_header__parent_ (){
__html += '<div class="x-header">\n';
__html += '\t...\n';
__html += '</div\n>';
}
同理,對於append模式,我們可以把它編譯成:
block_header__parent_();
block_header();
function block_header__parent_ (){
__html += '<div class="x-header">\n';
__html += '\t...\n';
__html += '</div\n>';
}
6、block進階:多級繼承(layout.html ← middle.html ← child.html)
逐級prepend或者append貌似不太好整,不過可以變換下思路,採用smarty中的block嵌套形式,比如:
<&block name="block_header" &>
我是header
<&block name="block_header_inner" &>
我是inner
<&/block&>
<&/block&>
所以,在middle.html中,可以先實現block_header,再在其內部繼續挖坑定義block_header_inner,比如:
<%function block_header(){%>
我是header
<%@ block="block_header_inner" %>
<%}%>
這樣,在子模板child.html中只需要實現middle.html中的block_header_inner即可:
<%function block_header_inner(){%>
我是子模板中的inner
<%}%>
7、如何保證子模板中不會出現父模板未定義的東西?
這個是必須得去保證的,可以這樣簡單處理:在extends標籤被替換的地方,強制加上:
return __html;
三、實踐一下
1、layout.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<header>
<%@ block="block_header" %>
</header>
<section>
<%@ block="block_content" {%>
<div>
這裡是父layout中的內容
</div>
<%}%>
</section>
<footer>
<%@ block="block_footer" %>
</footer>
</body>
</html>
2、child.html
<%@ extends="layout.html" %>
<%function block_content(prepend){%>
我是子模板中的內容
<%}%>
<%function block_footer(){%>
我是子模板中的footer
<%}%>
我是子模板中多餘的內容
3、編譯後的中間檔案
/*--/Users/zhaoxianlie/SourceCode/biz/trunk/demo/views/child.html--*/
exports.html = function ($_ROOT) {
return function ($_DATA) {
var __html = '';
__html += '';
__html += '<!doctype html><html> <head> ' +
'<meta charset="UTF-8"> <title>Document</title> ' +
'</head> <body> <header> ';
__html += ' </header> <section> ';
block_content();
block_content__parent_();
function block_content__parent_() {
__html += ' <div> 這裡是父layout中的內容 </div> ';
}
__html += ' </section> <footer> ';
block_footer();
__html += ' </footer> </body></html>';
return __html;
// 其實故事到這裡就結束了。。。
__html += '';
function block_content(prepend) {
__html += '我是子模板中的內容';
}
__html += '';
function block_footer() {
__html += '我是子模板中多餘的內容';
}
__html += ' haha';
return __html;
};
};
4、運行以後
我是子模板中的內容
這裡是父layout中的內容
我是子模板中的footer
差不多也就這點兒東西,就這麼個原理,之後的smarty項目就好遷移了。