vapor leaf

作者: 搞好关系 | 来源:发表于2018-10-04 23:08 被阅读5次

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

    相关文章

      网友评论

        本文标题:vapor leaf

        本文链接:https://www.haomeiwen.com/subject/fyqwoftx.html