# Leaf Overview Leaf is a powerful templating language with Swift-inspired syntax. You can use it to generate dynamic HTML pages for a front-end website or generate rich emails to send from an API. This guide will give you an overview of Leaf's syntax and the available tags. ## Template syntax Here is an example of a basic Leaf tag usage. ```leaf There are #count(users) users. ``` Leaf tags are made up of four elements: - Token `#`: This signals the leaf parser to begin looking for a tag. - Name `count`: that identifies the tag. - Parameter List `(users)`: May accept zero or more arguments. - Body: An optional body can be supplied to some tags. This is similar to Swift's trailing-closure syntax. 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: ```leaf #(variable) #embed("template") #set("title") { Welcome to Vapor } #count(friends) #for(friend in friends) {
#(number)
} ``` ## Usage Here are some common Leaf usage examples. ### Conditions Leaf is able to evaluate a range of conditions using its `#if` tag. For example, if you provide a variable it will check that variable exists in its context: ```leaf #if(title) { The title is #(title) } else { No title was provided. } ``` You can also write comparisons, for example: ```leaf #if(title == "Welcome") { This is a friendly web page. } else { No strangers allowed! } ``` If you want to use another tag as part of your condition, you should omit the `#` for the inner tag. For example: ```leaf #if(lowercase(title) == "welcome") { This is a friendly web page. } else { No strangers allowed! } ``` Just like in Swift, you can also use `else if` statement.s ```leaf #if(title == "Welcome") { This is a friendly web page. } else if (1 == 2) { What? } else { No strangers allowed! } ``` ### Loops If you provide an array of items, Leaf can loop over them and let you manipulate each item individually using its `#for` tag. For example, we could update our Swift code to provide a list of planets: ```swift struct SolarSystem: Codable { let planets = ["Venus", "Earth", "Mars"] } return try req.view().render(..., SolarSystem()) ``` We could then loop over them in Leaf like this: ```leaf Planets:Welcome to Vapor!
} #embed("master") ``` This stores some HTML in the context as `body` using `#set`. We then embed master.leaf which will render `body` along with any other context variables passed in from Swift. For example, master.leaf might look like this: ```leafWelcome to Vapor!
``` ### Comments You can write single or multiline comments with Leaf. They will be discarded when rendering the view. ```leaf #// Say hello to the user Hello, #(name)! ``` Multi-line comments are opened with `#/*` and closed with `*/`. ```leaf #/* Say hello to the user */ Hello, #(name)! ``` ### Other tags #### `#date` The `#date` tag formats dates into a readable string. ```swift render(..., ["now": Date()]) ``` ```leaf The time is #date(now) ``` You can pass a custom date formatter string as the second argument. See Swift's [`DateFormatter`](https://developer.apple.com/documentation/foundation/dateformatter) for more information. ```leaf The date is #date(now, "yyyy-MM-dd") ``` #### `#capitalize` The `#capitalize` tag uppercases the first letter of any string. ```leaf #capitalize(name) ``` #### `#contains` The `#contains` tag accepts an array and a value as its two parameters, and returns true if the array in parameter one contains the value in parameter two. ```leaf #if(contains(planets, "Earth")) { Earth is here! } else { Earth is not in this array. } ``` #### `#count` The `#count` tag returns the number of items in an array. For example: ```leaf Your search matched #count(matches) pages. ``` #### `#lowercase` The `#lowercase` tag lowercases all letters in a string. ```leaf #lowercase(name) ``` #### `#uppercase` The `#uppercase` tag uppercases all letters in a string. ```leaf #uppercase(name) ```