TemplateEngine
struct TemplateEngine
A template engine.
Methods
register_tester
pub fn register_tester(self, name: string, tester: |(Any?, [Any?])|: bool)
Register a tester to the template engine.
register_tester
pub fn register_tester(self, name: string, tester: |(Any?, [Any?])|: bool)
Register a tester to the template engine.
Register a tester to the template engine.
Example
nv
use std.template.TemplateEngine;
fn main() throws {
let engine = TemplateEngine.new();
engine.register_tester("greater", |value, args| {
let value = value?.(int) else {
return false;
};
let n = args[0]?.(int) else {
return false;
};
return value > n;
});
try engine.add("hello", `{% if value is greater(4) %}true{% else %}false{% endif %}`);
assert_eq try engine.render("hello", {"value": 5}), "true";
assert_eq try engine.render("hello", {"value": 4}), "false";
assert_eq try engine.render("hello", {"value": 3}), "false";
}
register_function
pub fn register_function(self, name: string, function: |(<string, Any?>)|: Any?)
Register a function to the template engine.
register_function
pub fn register_function(self, name: string, function: |(<string, Any?>)|: Any?)
Register a function to the template engine.
Register a function to the template engine.
Example
nv
use std.template.TemplateEngine;
fn main() throws {
let engine = TemplateEngine.new();
engine.register_function("repeat_string", |args| {
let value = args["value"]?.(string) else {
return nil;
};
let count = args["count"]?.(int) else {
return nil;
};
return value.repeat(count);
});
try engine.add("hello", `Hello, {{ repeat_string(value = "world", count = 3) }}!`);
let rendered = try engine.render("hello", {:} as <string, Any>);
assert_eq rendered, "Hello, worldworldworld!";
}
Register a filter to the template engine.
Register a filter to the template engine.
Register a filter to the template engine.
Example
nv
use std.template.TemplateEngine;
fn main() throws {
let engine = TemplateEngine.new();
engine.register_filter("repeat_string", |value, args| {
let value = value?.(string) else {
return nil;
};
let count = args["count"]?.(int) else {
return nil;
};
return value.repeat(count);
});
try engine.add("hello", "Hello, {{ name | repeat_string(count = 5) }}!");
let rendered = try engine.render("hello", {"name": "world"});
assert_eq rendered, "Hello, worldworldworldworldworld!";
}
render
pub fn render<T>(self, name: string, context: T): string throws TemplateError
Render a template and return a string.
render
pub fn render<T>(self, name: string, context: T): string throws TemplateError
Render a template and return a string.
Render a template and return a string.
Example
nv
use std.template.TemplateEngine;
fn main() throws {
let engine = TemplateEngine.new();
try engine.add("hello", "Hello, {{ name }}!");
let rendered = try engine.render("hello", { "name": "world" });
assert_eq rendered, "Hello, world!";
}
add
pub fn add(self, name: string, content: string) throws TemplateError
Add a template to the engine.
add
pub fn add(self, name: string, content: string) throws TemplateError
Add a template to the engine.
Add a template to the engine.
new
pub fn new(): TemplateEngine
Creates a new template engine.
new
pub fn new(): TemplateEngine
Creates a new template engine.
Creates a new template engine.