vapor leaf

Here is an example of basic Leaf tag usage

There are  #count(users)  users

Leaf tags are nde up of four elements:
. Token #: This signals the leaf parser to begin looking for tag.
. Name count: that identifies the tag.
. Parameter List (users):May acept zero or more args
There can be many different usages of these four elements depending on the tag's implementation. Let's look at a few examples of how Leaf's built-in tags might be used:

  #(variable)
  #embed("template")
  #set("title"){ Welcome to Vapor }
  #count(friends)
  #for(friend in friends) { <li> #(friend.name) </li>}

Leaf also supports many expressions you are familiar with in Swift.
. +
. >
. ==
. ||
. etc.

#if( 1+1 == 2){
  Hello!
}

Context

In the example from , we used a [String: String] dictionary to pass data to Leaf. Howver, you can pass anything that cnoforms to Encodable. It's actually prefferrd to use Encodable structs since [String:Any] is not supported

Loop

struct SolarSystem: Codable {
    let planets = ["Venus", "Earth", "Mars"]
}

return try req.view().render(..., SolarSystem())

We could then loop over them in Leaf like this:

Planets:
<ul>
#for(planet in planets) {
    <li>#(planet)</li>
}
</ul>

Leaf provides some extra variables inside a #for loop to give you more information about the loop's progress:

The isFirst variable is true when the current iteration is the first one.
The isLast variable is true when it's the last iteration.
The index variable will be set to the number of the current iteration, counting from 0.

#for(planet in planets) {
    #if(isFirst) { #(planet) is first! }
}

exaple:

 struct Header: Codable {
            var name: String
            var value: String
        }
        struct Headers: Codable {
            var headers = [Header]()
        }
        var headers: Headers =     Headers()
        let headList =  req.http.headers.map({ (name, value) -> Header in
            return Header(name: name, value: value)
        })
        
        headers.headers.append(contentsOf: headList)
        
      return  try req.view().render("leaf", headers)

Leaf

  #for(header in headers){
      <li>#(header.name) : #(header.value)</li>
}

Usage

if else

#if( lowercase(title) == "welcome" ){

}else{

}

Embedding templates

Leaf’s #embed tag allows you to copy the contents of one template into another. When use this, you should always omit the template file's .leaf extension.
child.leaf

#set(body){
<p>Welcome to Vapor!</p>
}

#embed("master")

master.lead

<html>
<head>
<title>#(title)</title>
</head>
<body>#get(body)</body>
</html>

使用:

        return try! req.view().render("child", ["title":"Hello "] )

【注意】master是一个模版而child才是需要渲染的leaf,引擎会将child.leaf中的set设置的标签映射到master模版中的对应的位置

comments

单行
#//Say hello
Hello #(name)!
多行

#/*
  Say hello to the user
*/

Hello #(name)!

date

The date is #date(now,"yyy--MM-dd")

contains

#if(contains(planets, "Earth")) {
    Earth is here!
} else {
    Earth is not in this array.
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容