警告
本節(jié)可能包含過(guò)時(shí)的信息氓英。
葉(Leaf)
歡迎來(lái)到Leaf。Leaf的目標(biāo)是成為一種簡(jiǎn)單的模板語(yǔ)言鹦筹,可以使生成視圖更容易铝阐。有很多偉大的模板語(yǔ)言,使用最適合你的盛龄,也許就是Leaf饰迹!葉的目標(biāo)如下:
- Small set of strictly enforced rules 小套嚴(yán)格執(zhí)行規(guī)則
- Consistency 一致性
- Parser first mentality 解析器第一心態(tài)
- Extensibility 可擴(kuò)展性
語(yǔ)法(Syntax)
結(jié)構(gòu)(Structure)
葉標(biāo)簽由4個(gè)元素組成: - Token:#
是令牌 - Name:標(biāo)識(shí)標(biāo)簽的string
- Parameter List:()
可以接受0或更多個(gè)參數(shù) - Body (optional(可選)):{}
必須與參數(shù)列表分隔開(kāi)一個(gè)空格
根據(jù)標(biāo)簽的實(shí)現(xiàn),這4個(gè)元素可以有很多不同的用法余舶。我們來(lái)看幾個(gè)例子啊鸭,說(shuō)明如何使用Leaf的內(nèi)置標(biāo)簽:
- #()
- #(variable)
- #import("template")
- #export("link") { <a href="#()"></a> }
- #index(friends, "0")
- #loop(friends, "friend") { <li>#(friend.name)</li> }
- #raw() { <a href="#raw">Anything goes!@#$%^&*</a> }
在HTML中使用#
令牌(Using the #
token in HTML)
#
令牌不能轉(zhuǎn)義。在葉子模板中,使用#()
或#raw() {}
標(biāo)簽輸出一個(gè)#
匿值。#()
=>#
原始 HTML(Raw HTML)
默認(rèn)情況下赠制,所有Leaf輸出都將被轉(zhuǎn)義。使用#raw() {}
標(biāo)簽進(jìn)行非轉(zhuǎn)義輸出挟憔。
#raw() { <a href="#link">Link</a> } => <a href="#link">Link</a>
重要钟些!確保您沒(méi)有使用
#raw() {}
帶有用戶輸入的標(biāo)簽。
鏈(Chaining)
雙重令牌:##
表示鏈绊谭。它可以應(yīng)用于任何標(biāo)準(zhǔn)標(biāo)簽政恍。如果以前的標(biāo)簽失敗,鏈接的標(biāo)簽將被給予運(yùn)行的機(jī)會(huì)达传。
#if(hasFriends) ##embed("getFriends")
Leaf的內(nèi)置標(biāo)簽(Leaf's built-in Tags)
令牌:#()
(Token: #()
)
#() #()hashtags #()FTW => # #Hashtags #FTW
原始:#raw() {}
(Raw: #raw() {}
)
#raw() {
Do whatever w/ #'s here, this code won't be rendered as leaf document and is not escaped.
It's a great place for things like Javascript or large HTML sections.
}
等于:#equal(lhs, rhs) {}
(Equal: #equal(lhs, rhs) {}
)
#equal(leaf, leaf) { Leaf == Leaf } => Leaf == Leaf
#equal(leaf, mustache) { Leaf == Mustache } =>
變量:#(variable)
(Variable:#(variable)
)
Hello, #(name)!
循環(huán):#loop(object, "index")
(Loop: #loop(object, "index")
)
#loop(friends, "friend") {
Hello, #(friend.name)!
}
索引:#index(object, _ index: Int|String)
(Index: #index(object, _ index: Int|String)
)
Hello, #index(friends, 0)!
Hello, #index(friends, "best")!
如果 - 其他:#if(bool) ##else() { this }
(If - Else: #if(bool) ##else() { this }
)
#if(entering) {
Hello, there!
} ##if(leaving) {
Goodbye!
} ##else() {
I've been here the whole time.
}
導(dǎo)入 Import: #import("template")
出口 Export: #export("template") { Leaf/HTML }
擴(kuò)展 Extend: #extend("template")
嵌入 Embed: #embed("template")
使用這些布局標(biāo)簽時(shí)迫筑,請(qǐng)省略模板文件的.leaf擴(kuò)展名。
/// base.leaf
<!DOCTYPE html>
#import("html")
/// html.leaf
#extend("base")
#export("html") { <html>#embed("body")</html> }
/// body.leaf
<body></body>
Leaf呈現(xiàn)html.leaf
為:
<!DOCTYPE html>
<html><body></body></html>
自定義標(biāo)簽(Custom Tags)
看一下現(xiàn)有的高級(jí)場(chǎng)景標(biāo)簽蒙保,讓我們一起來(lái)看一個(gè)基本的Index
例子辕棚。此標(biāo)簽將使用兩個(gè)參數(shù),數(shù)組和要訪問(wèn)的索引邓厕。
class Index: BasicTag {
let name = "index"
func run(arguments: [Argument]) throws -> Node? {
guard
arguments.count == 2,
let array = arguments[0].value?.nodeArray,
let index = arguments[1].value?.int,
index < array.count
else { return nil }
return array[index]
}
}
我們現(xiàn)在可以注冊(cè)這個(gè)標(biāo)簽在我們main.swift
文件:
if let leaf = drop.view as? LeafRenderer {
leaf.stem.register(Index())
}
并就像我們上面所做的那樣使用逝嚎。
注意:強(qiáng)烈不提倡在標(biāo)簽名稱(chēng)中使用非字母數(shù)字字符,并且在將來(lái)的Leaf版本中可能不允許使用邑狸。
語(yǔ)法突出顯示(Syntax Highlighting)
原子(Atom)
language-leaf
由ButkiewiczP提供
Xcode
目前不可能在Xcode中實(shí)現(xiàn)Leaf Syntax Highlighting懈糯,但是,使用Xcode的HTML語(yǔ)法著色可以幫助一點(diǎn)单雾。選擇一個(gè)或多個(gè)Leaf文件,然后選擇編輯器>語(yǔ)法著色> HTML她紫。您選擇的Leaf文件現(xiàn)在將使用Xcode的HTML語(yǔ)法著色硅堆。不幸的是,這樣做的有用性是有限的贿讹,因?yàn)?code>vapor xcode運(yùn)行時(shí)會(huì)關(guān)閉這個(gè)關(guān)聯(lián)渐逃。
似乎有一種辦法可以使Xcode文件關(guān)聯(lián)持續(xù)存在,但這需要更多的功夫民褂。
VS代碼(VS Code)
html-leaf 由FranciscoAmado提供
CLion & AppCode
一些初步的工作已經(jīng)完成了為CLion和AppCode實(shí)施一個(gè)Leaf插件茄菊,但缺乏對(duì)Java的技能和興趣已經(jīng)放慢了進(jìn)展!如果您有IntelliJ SDK的經(jīng)驗(yàn)赊堪,并希望幫助這一點(diǎn)面殖,在Vapor Slack上發(fā)送消息給Tom Holland 。