Template

class Template(template: String, replaceables: String) : Stack

A fast, lightweight string templating system with zero dependencies.

You supply a template String and an array of tokens to replace when calling .apply. The order of tokens supplied in the constructor corresponds to the order of strings supplied in apply().

The io.kweb.util.Template will compile your string into an executable stack, which generates output with extreme efficiency; the only string-matching performed is during io.kweb.util.Template compile, and it processes all tokens in a single, efficient iteration of the template String.

This ensures an absolute minimum of processing, and allows large templates with a large number of replacements to scale nicely.

Usage:

assert new Template("$1, $2!", "$1", "$2") .apply("Hello", "World") .equals("Hello, World!");

assert new Template("(<>[])$.toArray(new Object$.size())", "<>", "$") .apply("String", "myList") .equals("(String[])myList.toArray(new ObjectmyList.size())");

Author

"James X. Nelson (james@wetheinter.net)"

Constructors

Template
Link copied to clipboard
fun Template(template: String, vararg replaceables: String)

Types

Companion
Link copied to clipboard
object Companion

Functions

apply
Link copied to clipboard
open override fun apply(vararg args: String?): String
Applies the current template to the supplied arguments.
push
Link copied to clipboard
fun push(prefix: String?, pos: Int): Stack
Pushes a string constant and a pointer to a token's replacement position onto stack.

Properties

next
Link copied to clipboard
var next: Stack? = null
prefix
Link copied to clipboard
val prefix: String?