Kitura Logo


Setup environment

1. Create a directory:

mkdir ~/KituraMustacheExample

2. Change to new directory:

cd ~/KituraMustacheExample

3. Create a Kitura sever:

kitura init

kitura init creates, builds and generates a starter Kitura Xcode project and will take a couple of minutes to complete. This includes best-practice implementations of health checking and monitoring as well as configuration files to allow easy deployment to a Docker container, a Kubernetes cluster, or the IBM Cloud.

Add Kitura-MustacheTemplateEngine to the Kitura app

Add KituraMustache to the package dependencies:

.package(url: "", from: "1.0.0")

Add KituraMustache to the target dependencies:

.target(name: "Application", dependencies: [ "Kitura", "CloudEnvironment","SwiftMetrics","Health", "KituraMustache"]),

Our Package.swift should look like this:

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    name: "KituraMustacheExample",
    dependencies: [
        .package(url: "", .upToNextMinor(from: "2.2.0")),
        .package(url: "", .upToNextMinor(from: "1.7.1")),
        .package(url: "", .upToNextMinor(from: "6.0.0")),
        .package(url: "", from: "2.0.0"),
        .package(url: "", from: "0.0.0"),
        .package(url: "", from: "1.0.0"),
  targets: [
            .target(name: "KituraMustacheExample", dependencies: [ .target(name: "Application"), "Kitura" , "HeliumLogger"]),
            .target(name: "Application", dependencies: [ "Kitura", "CloudEnvironment","SwiftMetrics","Health", "KituraStencil"]),

            .testTarget(name: "ApplicationTests" , dependencies: [.target(name: "Application"), "Kitura","HeliumLogger" ])

Add a template file to the Kitura app

By default template engines look in a ./Views directory for the template files to use so we need to add this directory to our project. In a terminal window run the following:

mkdir ~/KituraMustacheExample/Views

We’ve added two new packages and a new directory to our project but if you open the KituraMustacheExample.xcodeproj that was generated by kitura init none of our additions will be visible within Xcode. To solve this we need to generate a new Xcode project so it will be aware of all we’ve added. Open a terminal and run the following:

1. Ensure you’re in the correct directory:

cd ~/KituraMustacheExample

2. Generate new Xcode project:

swift package generate-xcodeproj

3. Once the command has finished executing open the new project:

open KituraMustacheExample.xcodeproj

Now we can add a template file to our application:

1. In Xcode right-click on the Views folder and select New File.

2. Click on the new file (called File by default) and change the name to MustacheExample.mustache.

Add the following into the MustacheExample.mustache file:

Hello {{ name }}!

Register the template engine with a Router instance

We now have everything in place to start writing the code for our implementation of the template engines!

In Xcode open the Application.swift file found in Sources/Application/

The first step we do to do is import our packages so we can make use of the API they provide.

Below the line:

import Health

Add the following:

import KituraMustache

You may need to build your project before Xcode can find the package.

To use the template engine, we have to register it with a Router instance. This is done by calling the Router.add(templateEngine:) function.

In the postInit() function below the line:

initializeHealthRoutes(app: self)

Add the following:

router.add(templateEngine: MustacheTemplateEngine())

Rendering a template

We can render a template by calling the response.render(_:context) function, where context is the information the template engine will use to fill out the corresponding template files.

Below the line:

router.add(templateEngine: MustacheTemplateEngine())

add the following:

router.get("/hello") { request, response, next in
    try response.render("MustacheExample.mustache", context: ["name": "World!"]])

That's it! Three lines of code is all we need to render a template file!

To see the result we need to start the Kitura server, in Xcode we can do this using the shortcut cmd + R

Make sure your build scheme is correct! See Getting Started on how to do this!

Allow the project to accept incoming network connections if prompted

Then open:


In the browser we should see:

Hello World!
Slack icon