pub struct Template<'a> {
raw: String,
file_path: &'a str,
schema: Value,
shared: Shared,
time_start: Instant,
time_elapsed: Duration,
out: String,
}Fields§
§raw: String§file_path: &'a str§schema: Value§time_start: Instant§time_elapsed: Duration§out: StringImplementations§
Source§impl<'a> Template<'a>
A struct representing a template that can be rendered.
impl<'a> Template<'a>
A struct representing a template that can be rendered.
This struct is used to handle the rendering of templates.
Sourcepub fn new() -> Result<Self, String>
pub fn new() -> Result<Self, String>
Constructs a new Template instance with default settings.
It allows you to set up a template and schema with different types.
Sourcepub fn from_file_value(
file_path: &'a str,
schema: Value,
) -> Result<Self, String>
pub fn from_file_value( file_path: &'a str, schema: Value, ) -> Result<Self, String>
Constructs a new Template instance from a file path and a JSON schema.
§Arguments
file_path- A reference to the path of the file containing the template content.schema- A JSON value representing the custom schema to be used with the template.
§Returns
A Result containing the new Template instance or an error message if:
- The file cannot be read.
Sourcepub fn set_src_str(&mut self, source: &str)
pub fn set_src_str(&mut self, source: &str)
Sets the content of the template from a string.
§Arguments
source- A reference to the new string content to be set as the raw content.
Sourcepub fn merge_schema_value(&mut self, schema: Value)
pub fn merge_schema_value(&mut self, schema: Value)
Merges the provided JSON value with the current schema.
§Arguments
schema- The JSON Value to be merged with the current schema.
Sourcepub fn from_file_msgpack(
file_path: &'a str,
bytes: &[u8],
) -> Result<Self, String>
pub fn from_file_msgpack( file_path: &'a str, bytes: &[u8], ) -> Result<Self, String>
Constructs a new Template instance from a file path and MessagePack schema bytes.
§Arguments
file_path- A reference to the path of the file containing the template content.bytes- A byte slice containing the MessagePack schema.
§Returns
A Result containing the new Template instance or an error message if:
- The template file cannot be read.
- The MessagePack data is invalid.
§Example
use neutralts::Template;
let bytes = vec![129, 164, 100, 97, 116, 97, 129, 163, 107, 101, 121, 165, 118, 97, 108, 117, 101];
let template = Template::from_file_msgpack("template.ntpl", &bytes).unwrap();Sourcepub fn merge_schema_msgpack_path(
&mut self,
msgpack_path: &str,
) -> Result<(), String>
pub fn merge_schema_msgpack_path( &mut self, msgpack_path: &str, ) -> Result<(), String>
Merges the schema from a MessagePack file with the current template schema.
§Arguments
msgpack_path- A reference to the path of the file containing the MessagePack schema.
§Returns
A Result indicating success or an error message if:
- The file cannot be read.
- The file’s content is not a valid MessagePack.
§Example
use neutralts::Template;
let mut template = Template::new().unwrap();
template.merge_schema_msgpack_path("extra_data.msgpack").unwrap();Sourcepub fn merge_schema_msgpack(&mut self, bytes: &[u8]) -> Result<(), String>
pub fn merge_schema_msgpack(&mut self, bytes: &[u8]) -> Result<(), String>
Merges the schema from MessagePack bytes with the current template schema.
§Arguments
bytes- A byte slice containing the MessagePack schema.
§Returns
A Result indicating success or an error message if:
- The bytes are not a valid MessagePack.
§Example
use neutralts::Template;
let mut template = Template::new().unwrap();
let bytes = vec![129, 164, 100, 97, 116, 97, 129, 163, 107, 101, 121, 165, 118, 97, 108, 117, 101];
template.merge_schema_msgpack(&bytes).unwrap();Sourcepub fn render(&mut self) -> String
pub fn render(&mut self) -> String
Renders the template content.
This function initializes the rendering process. The resulting output is returned as a string.
§Returns
The rendered template content as a string.
Sourcepub fn render_once(&mut self) -> String
pub fn render_once(&mut self) -> String
Renders the template content without cloning the schema.
This is an optimized version of render() that takes ownership of the schema
instead of cloning it. Use this when you only need to render once per template
instance, which is the most common use case in web applications.
§When to Use
- Single render per request: Most web applications create a template, render it once,
and discard it. This is the ideal use case for
render_once(). - Large schemas: When your schema contains thousands of keys, the performance
improvement can be 5-10x faster than
render(). - Memory-constrained environments: Avoids the memory spike of cloning large schemas.
§When NOT to Use
- Multiple renders: If you need to render the same template multiple times with
the same schema, use
render()instead. - Template reuse: After
render_once(), the template cannot be reused because the schema is consumed.
§Performance
Benchmarks show significant improvements for large schemas:
- 100 keys: ~3.7x faster
- 500 keys: ~7x faster
- 1000+ keys: ~10x faster
§Post-Call Behavior
After calling this method, the template’s schema will be empty ({}) and subsequent
calls to render() or render_once() will produce empty output for schema variables.
The template struct itself remains valid but should be discarded after use.
§Example
use neutralts::Template;
let schema = serde_json::json!({
"data": {
"title": "Hello World"
}
});
let mut template = Template::new().unwrap();
template.merge_schema_value(schema);
template.set_src_str("{:;title:}");
// Single render - use render_once() for best performance
let output = template.render_once();
assert!(output.contains("Hello World"));
// Template should NOT be reused after render_once()
// Create a new Template instance for the next render§Returns
The rendered template content as a string.
fn init_render(&mut self) -> BlockInherit
fn init_render_once(&mut self) -> BlockInherit
fn ends_render(&mut self)
fn set_status_code(&mut self)
fn set_moveto(&mut self)
fn replacements(&mut self)
Sourcepub fn get_status_code(&self) -> &String
pub fn get_status_code(&self) -> &String
Retrieves the status code.
The status code is “200” unless “exit”, “redirect” is used or the template contains a syntax error, which will return a status code of “500”. Although the codes are numeric, a string is returned.
§Returns
A reference to the status code as a string.
Sourcepub fn get_status_text(&self) -> &String
pub fn get_status_text(&self) -> &String
Retrieves the status text.
It will correspond to the one set by the HTTP protocol.
§Returns
A reference to the status text as a string.
Sourcepub fn get_status_param(&self) -> &String
pub fn get_status_param(&self) -> &String
Retrieves the status parameter.
Some statuses such as 301 (redirect) may contain additional data, such as the destination URL, and in similar cases “param” will contain that value.
§Returns
A reference to the status parameter as a string.
Sourcepub fn has_error(&self) -> bool
pub fn has_error(&self) -> bool
Checks if there is an error.
If any error has occurred, in the parse or otherwise, it will return true.
§Returns
A boolean indicating whether there is an error.
Sourcepub fn get_error(&self) -> Value
pub fn get_error(&self) -> Value
Get bifs errors list
§Returns
Value: A clone of the value with the list of errors in the bifs during rendering.