0

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?

MacAvon
  • 15
  • 3

1 Answers1

1

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.

Vadim Eisenberg
  • 3,337
  • 1
  • 18
  • 14
  • Thank you for confirming that I need to make changes to `class StencilTemplateEngine`. I did it slightly differently from the way you suggest and now have a working solution. – MacAvon Nov 14 '16 at 21:15
  • @MacAvon The rationale here is that once you customize Stencil Template Engine, e.g. with custom tags, you can consider it as a different template engine. So you can create your own _Kitura-MyCustomizedStencilTemplateEngine_ repository, with its own versioning etc., and use it for your multiple Kitura apps. _Kitura-MyCustomizedStencilTemplateEngine_ will implement [Kitura TemplateEngine protocol](https://github.com/IBM-Swift/Kitura-TemplateEngine/blob/master/Sources/KituraTemplateEngine/TemplateEngine.swift) and add whatever customizations are required for Stencil, for all your Kitura apps. – Vadim Eisenberg Nov 15 '16 at 06:43