Kitura-StencilTemplateEngine icon indicating copy to clipboard operation
Kitura-StencilTemplateEngine copied to clipboard

Support for custom filters

Open dillan opened this issue 5 years ago • 1 comments

Does Kitura-StencilTemplateEngine include support for custom stencil filters?

From the Stencil documentation:

You can build your own custom filters and tags and pass them down while rendering your template. Any custom filters or tags must be registered with a extension which contains all filters and tags available to the template.

let ext = Extension()
// Register your filters and tags with the extension
let environment = Environment(extensions: [ext])
try environment.renderTemplate(name: "example.html")

Registering custom filters:

ext.registerFilter("double") { (value: Any?) in 
  if let value = value as? Int {
    return value * 2 
  }
  return value 
}

Ideally, I'd like to use a custom filter with HTMLEntities since I don't want to store HTML rendering artifacts in my database. I could also extend my model objects to handle the HTML Entities on values that are added to the stencil context, but it feels like a custom filter is matches the spirit of using stencil.

dillan avatar Aug 22 '19 16:08 dillan

It does, you can see my examples here: https://github.com/kylebshr/mustache.cc/blob/master/Sources/mustache/Application.swift#L42

kylebshr avatar Nov 20 '19 03:11 kylebshr