Vapor 2.0 - Leaf概述(Leaf Overview

2017-08-14  本文已影响0人  韩云智VL

前往 Vapor 2.0 - 文档目录

警告
本节可能包含过时的信息。

叶(Leaf)

欢迎来到Leaf。Leaf的目标是成为一种简单的模板语言,可以使生成视图更容易。有很多伟大的模板语言,使用最适合你的,也许就是Leaf!叶的目标如下:

语法(Syntax)

结构(Structure)

叶标签由4个元素组成: - Token:#
是令牌 - Name:标识标签的string- Parameter List:()可以接受0或更多个参数 - Body (optional(可选)):{}必须与参数列表分隔开一个空格

根据标签的实现,这4个元素可以有很多不同的用法。我们来看几个例子,说明如何使用Leaf的内置标签:

在HTML中使用#令牌(Using the # token in HTML)

#令牌不能转义。在叶子模板中,使用#()#raw() {}标签输出一个##()=>#

原始 HTML(Raw HTML)

默认情况下,所有Leaf输出都将被转义。使用#raw() {}标签进行非转义输出。
#raw() { <a href="#link">Link</a> } => <a href="#link">Link</a>

重要!确保您没有使用#raw() {}带有用户输入的标签。

链(Chaining)

双重令牌:##表示链。它可以应用于任何标准标签。如果以前的标签失败,链接的标签将被给予运行的机会。

#if(hasFriends) ##embed("getFriends")

Leaf的内置标签(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)!
循环:#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.
}
导入 Import: #import("template")
出口 Export: #export("template") { Leaf/HTML }
扩展 Extend: #extend("template")
嵌入 Embed: #embed("template")

使用这些布局标签时,请省略模板文件的.leaf扩展名。

/// base.leaf
<!DOCTYPE html>
#import("html")

/// html.leaf
#extend("base")

#export("html") { <html>#embed("body")</html> }

/// body.leaf
<body></body>

Leaf呈现html.leaf为:

<!DOCTYPE html>
<html><body></body></html>

自定义标签(Custom Tags)

看一下现有的高级场景标签,让我们一起来看一个基本的Index例子。此标签将使用两个参数,数组和要访问的索引。

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]
    }
}

我们现在可以注册这个标签在我们main.swift文件:

if let leaf = drop.view as? LeafRenderer {
    leaf.stem.register(Index())
}

并就像我们上面所做的那样使用。

注意:强烈不提倡在标签名称中使用非字母数字字符,并且在将来的Leaf版本中可能不允许使用。

语法突出显示(Syntax Highlighting)

原子(Atom)

language-leaf
由ButkiewiczP提供

Xcode

目前不可能在Xcode中实现Leaf Syntax Highlighting,但是,使用Xcode的HTML语法着色可以帮助一点。选择一个或多个Leaf文件,然后选择编辑器>语法着色> HTML。您选择的Leaf文件现在将使用Xcode的HTML语法着色。不幸的是,这样做的有用性是有限的,因为vapor xcode运行时会关闭这个关联。

似乎有一种办法可以使Xcode文件关联持续存在,但这需要更多的功夫。

VS代码(VS Code)

html-leaf 由FranciscoAmado提供

CLion & AppCode

一些初步的工作已经完成了为CLion和AppCode实施一个Leaf插件,但缺乏对Java的技能和兴趣已经放慢了进展!如果您有IntelliJ SDK的经验,并希望帮助这一点,在Vapor Slack上发送消息给Tom Holland 。

上一篇 下一篇

猜你喜欢

热点阅读