Struct dojang::dojang::Dojang[][src]

pub struct Dojang { /* fields omitted */ }
Expand description

HTML template rendering engine that should be constructed for once.

Implementations

Creates a template engine.

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);

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.