Search code examples
kitura

Using custom Stencil tag in Kitura


Following the instructions in the Stencil documentation, I registered a simple custom tag:

let ns = Namespace()

ns.registerSimpleTag("contact") { context in
  return "<a href=\"/contact\">contact us</a>"
}

but I can't see how to pass the namespace to render, as response.render only takes a dictionary, not an actual Context object.

What am I missing? Is there a pre-existing namespace somewhere I should be using instead of creating one myself?


Solution

  • You can fork https://github.com/IBM-Swift/Kitura-StencilTemplateEngine and change KituraStencilTemplateEngine.swift.

    Define your ns namespace in StencilTemplateEngine.render() and change the return code line to be:

    return try template.render(Context(dictionary: context), namespace: ns)
    

    Then add a tag to your fork and use your fork as a dependency in Package.swift.