Leaf

Leaf is a templating language that integrates with Futures, Reactive Streams and Codable. This section outlines how to import the Leaf package into a Vapor project.

Example Folder Structure

  1. Hello
  2. ├── Package.resolved
  3. ├── Package.swift
  4. ├── Public
  5. ├── Resources
  6. ├── Views
  7. └── hello.leaf
  8. ├── Public
  9. ├── images (images resources)
  10. ├── styles (css resources)
  11. ├── Sources
  12. ├── App
  13. ├── boot.swift
  14. ├── configure.swift
  15. └── routes.swift
  16. └── Run
  17. └── main.swift
  18. ├── Tests
  19. ├── AppTests
  20. └── AppTests.swift
  21. └── LinuxMain.swift
  22. └── LICENSE

Adding Leaf to your project

The easiest way to use Leaf with Vapor is to include the Leaf repository as a dependency in Package.swift:

  1. // swift-tools-version:4.0
  2. import PackageDescription
  3. let package = Package(
  4. name: "project1",
  5. dependencies: [
  6. // 💧 A server-side Swift web framework.
  7. .package(url: "https://github.com/vapor/vapor.git", .branch("beta")),
  8. .package(url: "https://github.com/vapor/leaf.git", .branch("beta")),
  9. ],
  10. targets: [
  11. .target(
  12. name: "App",
  13. dependencies: ["Vapor", "Leaf"]
  14. ),
  15. .target(name: "Run", dependencies: ["App"]),
  16. .testTarget(name: "AppTests", dependencies: ["App"]),
  17. ]
  18. )

The Leaf package adds Leaf to your project, but to configure it for use you must modify configure.swift:

  1. Add import Leaf to the top of the file so that Leaf is available to use. You will also need to add this to any file that will render templates.
  2. Add try services.register(LeafProvider()) to the configure() function so that routes may render Leaf templates as needed.

Syntax Highlighting

You may also wish to install one these third-party packages that provide support for syntax highlighting in Leaf templates.

Atom

language-leaf by ButkiewiczP

Xcode

It is not currently possible to implement Leaf Syntax Highlighting in Xcode, however, using Xcode’s HTML Syntax Coloring can help a bit. Select one or more Leaf files and then choose Editor > Syntax Coloring > HTML. Your selected Leaf files will now use Xcode’s HTML Syntax Coloring. Unfortunately the usefulness of this is limited because this association will be removed when vapor xcode is run.

There appears to be a way to make Xcode file associations persist but that requires a bit more kung-fu.

VS Code

html-leaf by FranciscoAmado

CLion & AppCode

Some preliminary work has been done to implement a Leaf Plugin for CLion & AppCode but lack of skill and interest in Java has slowed progress! If you have IntelliJ SDK experience and want to help with this, message Tom Holland on Vapor Slack