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.

Arguments

  • function_name - Name of the function.
  • function - The body of the function. Function must be taking serde_json::Value as a parameters and return serde_json::Value. Also, the function must be one of enum values defined in FunctionContainer, which are categorized by the number of parameters.

For example, if the function takes 2 params, it must be FunctionContainer::F2.

Examples

use serde_json::Value;
let mut dojang = dojang::Dojang::new();

// Register a function that takes two numeric values and returns the sum of it.
dojang.add_function("func".to_string(), dojang::FunctionContainer::F2(|a: Value, b: Value| -> Value {
       Value::Number(serde_json::Number::from(
           a.as_i64().unwrap() + b.as_i64().unwrap(),
       ))
   }));

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.