Struct dojang::dojang::Dojang [−][src]
pub struct Dojang { /* fields omitted */ }
Expand description
HTML template rendering engine that should be constructed for once.
Implementations
Adds a template file to the engine.
If there is already an existing template with same name, this will return error.
Arguments
file_name
- Name of the template file. Template datas are identified by this name.template
- Actual template data. Should be using EJS syntax.
Examples
let mut dojang = dojang::Dojang::new(); // Constructs the template "tmpl" with the content "<%= 1 + 1 %>". dojang.add("tmpl".to_string(), "<%= 1 + 1 %>".to_string());
Adds a function that can be used in the template.
If there is already an existing function with same name, this will return error. Use the appropriate function based on the number of parameters that the function take. (e.g for functions taking 2 params, use add_function_2). Functions with 4 params are supported at max.
Note that the parameter of the function must be convertible to Value. Supported types are String, i64, f64 and boolean.
Arguments
function_name
- Name of the function.function
- The body of the function.
Examples
use serde_json::Value; use dojang::dojang::Dojang; fn func(a: i64) -> i64 { a + 1 } fn func2(mut a: String, b: String) -> String { a.push_str(&b); a.push_str("hi"); a } let mut dj = Dojang::new(); dj.add_function_1("func".to_string(), func); dj.add_function_2("func2".to_string(), func2);
pub fn add_function_2<T1: 'static, T2: 'static, V: 'static>(
&mut self,
function_name: String,
function: fn(_: T1, _: T2) -> V
) -> Result<&Self, String> where
T1: From<Operand>,
T2: From<Operand>,
V: Into<Operand>,
pub fn add_function_3<T1: 'static, T2: 'static, T3: 'static, V: 'static>(
&mut self,
function_name: String,
function: fn(_: T1, _: T2, _: T3) -> V
) -> Result<&Self, String> where
T1: From<Operand>,
T2: From<Operand>,
T3: From<Operand>,
V: Into<Operand>,
pub fn add_function_4<T1: 'static, T2: 'static, T3: 'static, T4: 'static, V: 'static>(
&mut self,
function_name: String,
function: fn(_: T1, _: T2, _: T3, _: T4) -> V
) -> Result<&Self, String> where
T1: From<Operand>,
T2: From<Operand>,
T3: From<Operand>,
T4: From<Operand>,
V: Into<Operand>,
Load files under the provided directory as templates.
Note that it does not recursively visit every underlying directories. Only the files that live in the current directory will be added to the engine.
If the file is not readable, then it will be ignored (will show an error message)
Arguments
dir_name
- Name of the directory.
Examples
let mut dojang = dojang::Dojang::new(); // Add every files under ./tests as a template. dojang.load("./tests");
Render the page with the provided context.
Arguments
file_name
: Name of the template file that should be rendered.value
: JSON value that is provided as a context. Note that this function consumes the json data.
Examples
let mut dojang = dojang::Dojang::new(); // Render 'template_file' with the provided context. dojang.load("./tests").unwrap().render("template_file", serde_json::from_str(r#"{ "test" : { "title" : "Welcome to Dojang"} }"#).unwrap());
Auto Trait Implementations
impl !RefUnwindSafe for Dojang
impl !UnwindSafe for Dojang