Basics

Welcome to Leaf. Leaf’s goal is to be a simple templating language that can make generating views easier. There are plenty of great templating languages, so use what’s best for you – maybe that’s Leaf! The goals of Leaf are:

  • Small set of strictly enforced rules
  • Consistency
  • Parser first mentality
  • Extensibility
  • Asynchronous and reactive

Rendering a template

Once you have Leaf installed, you should create a directory called “Resources” inside your project folder, and inside that create another directory called “Views”. This Resources/Views directory is the default location for Leaf templates, although you can change it if you want.

Firstly, import Leaf to routes.swift

  1. import Leaf

Then, to render a basic Leaf template from a route, add this code:

  1. router.get { req -> Future<View> in
  2. let leaf = try req.make(LeafRenderer.self)
  3. let context = [String: String]()
  4. return try leaf.render("home", context)
  5. }

That will load home.leaf in the Resources/Views directory and render it. The context dictionary is there to let you provide custom data to render inside the template, but you might find it easier to use codable structs instead because they provide extra type safety. For example:

  1. struct HomePage: Codable {
  2. var title: String
  3. var content: String
  4. }

Async

Leaf’s engine is completely reactive, supporting both streams and futures. One of the only ones of its kind.

When working with Future results, simply pass the Future in your template context. Streams that carry an encodable type need to be encoded before they’re usable within Leaf.

  1. struct Profile: Codable {
  2. var friends: EncodableStream
  3. var currentUser: Future<User>
  4. }

In the above context, the currentUser variable in Leaf will behave as being a User type. Leaf will not read the user Future if it’s not used during rendering.

EncodableStream will behave as an array of LeafData, only with lower memory impact and better performance. It is recommended to use EncodableStream for (large) database queries.

  1. Your name is #(currentUser.name).
  2. #for(friend in friends) {
  3. #(friend.name) is a friend of you.
  4. }

Template syntax

Structure

Leaf tags are made up of four elements:

  • Token: # is the token
  • Name: A string that identifies the tag
  • Parameter List: () May accept 0 or more arguments
  • Body (optional): {} Must be separated from the parameter list by a space

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

Working with context

In our Swift example from earlier, we used an empty [String: String] dictionary for context, which passes no custom data to Leaf. To try rendering content, use this code instead:

  1. let context = ["title": "Welcome", "message": "Vapor and Leaf work hand in hand"]
  2. return try leaf.make("home", context)

That will expose title and message to our Leaf template, which can then be used inside tags. For example:

  1. <h1>#(title)</h1>
  2. <p>#(message)</p>

Checking 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:

  1. #if(title) {
  2. The title is #(title)
  3. } else {
  4. No title was provided.
  5. }

You can also write comparisons, for example:

  1. #if(title == "Welcome") {
  2. This is a friendly web page.
  3. } else {
  4. No strangers allowed!
  5. }

If you want to use another tag as part of your condition, you should omit the # for the inner tag. For example:

  1. #if(lowercase(title) == "welcome") {
  2. This is a friendly web page.
  3. } else {
  4. No strangers allowed!
  5. }

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 names in a team:

  1. let context = ["team": ["Malcolm", "Kaylee", "Jayne"]]

We could then loop over them in Leaf like this:

  1. #for(name in team) {
  2. <p>#(name) is in the team.</p>
  3. }

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

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

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.

Embedding is useful for copying in a standard piece of content, for example a page footer or advert code:

  1. #embed("footer")

This tag is also useful for building one template on top of another. For example, you might have a master.leaf file that includes all the code required to lay out your website – HTML structure, CSS and JavaScript – with some gaps in place that represent where page content varies.

Using this approach, you would construct a child template that fills in its unique content, then embeds the parent template that places the content appropriately.

For example, you might create a child.leaf template like this:

  1. #set("body") {
  2. <p>Welcome to Vapor!</p>
  3. }
  4. #embed("master")

That configures one item of context, body, but doesn’t display it directly. Instead, it embeds master.leaf, which can render body along with any other context variables passed in from Swift. For example, master.leaf might look like this:

  1. <html>
  2. <head><title>#(title)</title></head>
  3. <body>#get(body)</body>
  4. </html>

When given the context ["title": "Hi there!"], child.leaf will render as follows:

  1. <html>
  2. <head><title>Hi there!</title></head>
  3. <body><p>Welcome to Vapor!</p></body>
  4. </html>

Other tags

#capitalize

The #capitalize tag uppercases the first letter of any string. For example, “taylor” will become “Taylor”.

  1. #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. For example, given the array team:

  1. #if(contains(team, "Jayne")) {
  2. You're all set!
  3. } else {
  4. You need someone to do PR.
  5. }

#count

The #count tag returns the number of items in an array. For example:

  1. Your search matched #count(matches) pages.

#lowercase

The #lowercase tag lowercases all letters in a string. For example, “Taylor” will become “taylor”.

  1. #lowercase(name)

#uppercase

The #uppercase tag uppercases all letters in a string. For example, “Taylor” will become “TAYLOR”.

  1. #uppercase(name)