A templating engine for Kitura that uses Mustache-based templates.
Kitura-MustacheTemplateEngine is a plugin for Kitura Template Engine for using GRMustache with the Kitura server framework. This makes it easy to use Mustache templating, with a Kitura server, to create an HTML page with integrated Swift variables.
Mustache Template File
The template file is basically HTML with gaps where we can insert code and variables. GRMustache is a templating language used to write a template file and Kitura-MustacheTemplateEngine can use any standard Mustache template.
Mustache manual provides documentation and examples on how to write a Mustache template File.
By default the Kitura Router will look in the Views folder for Mustache template files with the extension .mustache.
Usage
Add dependencies
Add the Kitura-MustacheTemplateEngine package to the dependencies within your application’s Package.swift file. Substitute "x.x.x" with the latest Kitura-MustacheTemplateEnginerelease.
Kitura-MustacheTemplateEngine
A templating engine for Kitura that uses Mustache-based templates.
Kitura-MustacheTemplateEngine is a plugin for Kitura Template Engine for using GRMustache with the Kitura server framework. This makes it easy to use Mustache templating, with a Kitura server, to create an HTML page with integrated Swift variables.
Mustache Template File
The template file is basically HTML with gaps where we can insert code and variables. GRMustache is a templating language used to write a template file and Kitura-MustacheTemplateEngine can use any standard Mustache template.
Mustache manual provides documentation and examples on how to write a Mustache template File.
By default the Kitura Router will look in the
Views
folder for Mustache template files with the extension.mustache
.Usage
Add dependencies
Add the
Kitura-MustacheTemplateEngine
package to the dependencies within your application’sPackage.swift
file. Substitute"x.x.x"
with the latestKitura-MustacheTemplateEngine
release.Add
KituraMustache
to your target’s dependencies:Import package
Example
The following example takes a server generated using
kitura init
and modifies it to serve Mustache-formatted text from a.mustache
file.The files which will be edited in this example, are as follows:
The
Views
folder andExample.mustache
file will be created later on in this example, since they are not initialized bykitura init
.Dependencies
Add the dependencies to your
Package.swift
file (as defined in Add dependencies above).Application.swift
Inside the
Application.swift
file, add the following code to render theExample.mustache
template file on the “/winner” route:Add the following code inside the
postInit()
function:Example.mustache
Create the
Views
folder and put the following Mustache template code into a file calledExample.mustache
:This example is adapted from the Mustache manual code to congratulate the winner of a contest.
Run the application and once the server is running, go to http://localhost:8080/winner to view the rendered Mustache template.
API documentation
For more information visit our API reference.
Community
We love to talk server-side Swift, and Kitura. Join our Slack to meet the team!
License
This library is licensed under Apache 2.0. Full license text is available in LICENSE.