TemplateProcessor

class TemplateProcessor(text: String, values: MutableMap<String, Any> = mutableMapOf(), conditionals: MutableMap<String, Boolean> = mutableMapOf(), iterables: MutableMap<String, Iterable<Any>> = mutableMapOf())

A builder-like processor for a simple template engine with three basic features:

  • Values: replace a placeholder in the template with a value. Values in templates are wrapped by double square brackets: [[NAME]].

  • Conditionals: show or hide fragments of the template code. The fragment in the template must be fenced by [[if:NAME]] and [[endif:NAME]]. An inverted (not) placeholder is fenced by [[if:!NAME]] and [[endif:!NAME]].

  • Iterables: repeat the content in their fragment as many times as the iterable's size, while replacing the placeholder with the current item during each iteration. The fragment in the template must be fenced by [[for:NAME]] and [[endfor:NAME]], and the iterated value appears like a normal value: [[NAME]].

For example, an HTML wrapper may add <html><head>...</head><body>...</body></html>, with the content injected in body. An example template resource can be found in resources/render/html-wrapper.html.template

Parameters

text

text or code of the template

Constructors

Link copied to clipboard
constructor(text: String, values: MutableMap<String, Any> = mutableMapOf(), conditionals: MutableMap<String, Boolean> = mutableMapOf(), iterables: MutableMap<String, Iterable<Any>> = mutableMapOf())

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun conditional(conditional: String, value: Boolean): TemplateProcessor

Adds a conditional variable that shows or removes fragments of the template code. The fragment in the template must be fenced by [[if:NAME]] and [[endif:NAME]]. An inverted (not) placeholder is also injected and can be accessed via [[if:!NAME]].

Link copied to clipboard

Adds a reference to a content placeholder in the template code. This is used to inject rendered code in a template.

Link copied to clipboard

Creates a copy of this template processor with the same injected properties.

Link copied to clipboard
fun iterable(placeholder: String, iterable: Iterable<Any>): TemplateProcessor

Adds an iterable to replace a placeholder in the template code. The placeholder in the template must be fenced by [[for:NAME]] and [[endfor:NAME]], and the iterated value appears like a normal value: [[NAME]].

Link copied to clipboard
fun optionalValue(placeholder: String, value: Any?): TemplateProcessor

Adds both a conditional to check if value is not null, and a value to replace the placeholder with the non-null value.

Link copied to clipboard
Link copied to clipboard
fun value(placeholder: String, value: Any): TemplateProcessor

Adds a reference to a placeholder in the template code. The placeholder in the template must be wrapped by double square brackets.