StencilTemplateEngine
public class StencilTemplateEngine : TemplateEngine
A TemplateEngine for Kitura that uses Stencil for templating.
The file extension for templates using this engine is stencil. If the file extension of the template specified in the call to response.render matches, this template engine will be invoked. If no extension is specified, and this engine has been set as the default via router.setDefault(templateEngine:), the extension will be applied automatically.
Usage Example:
router.add(templateEngine: StencilTemplateEngine())
// An example of using a dictionary of [String: Any] parameters to be rendered
router.get("/hello") { request, response, next in
try response.render("StencilExample.stencil", context: ["name": "World!"]])
next()
}
A second example, using type-safe templating. For more information, see: https://developer.ibm.com/swift/2018/05/31/type-safe-templating/
// A codable type containing structured data to be used in our template
struct Friend: Codable {
let firstName: String
let lastName: String
}
// Structured data that we wish to render
let friends = [Friend(firstName: "Jack", lastName: "Sparrow"), Friend(firstName: "Captain", lastName: "America")]
// An example of using type-safe templating to render data from a Swift type
router.get("/friends") { request, response, next in
try response.render("MyStencil.stencil", with: friends, forKey: "friends")
next()
}
-
The file extension of files rendered by the KituraStencil template engine.
Declaration
Swift
public let fileExtension: String -
Initializes a KituraStencil template engine.
Declaration
Swift
public init(extension: Extension = Extension())Parameters
extensionAn optional Stencil
Extensionfor customizing the underlying template engine. -
Defines the filesystem paths where your Stencil templates can be located. Note that Kitura calls this function for you with a default path of
./Views/or you can customize this by setting therouter.viewsPathproperty.Declaration
Swift
public func setRootPaths(rootPaths: [String])Parameters
rootPathsThe paths to be searched for Stencil templates.
-
This function is deprecated. Use
render(filePath:context:options:templateName:)instead.Declaration
Swift
public func render(filePath: String, context: [String : Any]) throws -> String -
Take a template file and a set of “variables” in the form of a context and generate content to be sent back to the client. Note that this function is called by Kitura when you call
response.render(_:context:options:).Declaration
Swift
public func render(filePath: String, context: [String: Any], options: RenderingOptions, templateName: String) throws -> StringParameters
filePathThe path of the template file to use when generating the content.
contextA set of variables in the form of a Dictionary of Key/Value pairs, that can be used when generating the content.
optionsUnused by this templating engine.
templateNameThe name of the template.
-
Take a template file and an
Encodabletype and generate the content to be sent back to the client. Note that this function is called by Kitura when you callresponse.render(_:with:forKey:options:).Declaration
Swift
public func render<T: Encodable>(filePath: String, with value: T, forKey key: String?, options: RenderingOptions, templateName: String) throws -> StringParameters
filePathThe path of the template file to use when generating the content.
withA value that conforms to
Encodablewhich is used to generate the content.forKeyA value used to match the
Encodablevalues to the correct variable in a template file. TheforKeyvalue should match the desired variable in the template file.optionsUnused by this templating engine.
templateNameThe name of the template.
View on GitHub
StencilTemplateEngine Class Reference