Struct Template

Source
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§shared: Shared§time_start: Instant§time_elapsed: Duration§out: String

Implementations§

Source§

impl<'a> Template<'a>

A struct representing a template that can be rendered.

This struct is used to handle the rendering of templates.

Source

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.

Source

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.
Source

pub fn set_src_path(&mut self, file_path: &'a str) -> Result<(), String>

Sets the source path of the template.

§Arguments
  • file_path - A reference to the path of the file containing the template content.
§Returns

A Result indicating success or an error message if the file cannot be read

Source

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.
Source

pub fn merge_schema_path(&mut self, schema_path: &str) -> Result<(), String>

Merges the schema from a file with the current template schema.

§Arguments
  • schema_path - A reference to the path of the file containing the schema content.
§Returns

A Result indicating success or an error message if:

  • The file cannot be read.
  • The file’s content is not a valid JSON string.
Source

pub fn merge_schema_str(&mut self, schema: &str) -> Result<(), String>

Merges the schema from a JSON string with the current template schema.

§Arguments
  • schema - A reference to the JSON string of the schema content.
§Returns

A Result indicating success or an error message if:

  • The file’s content is not a valid JSON string.
Source

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.
Source

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

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

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

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.

Source

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.

Source

fn init_render(&mut self) -> BlockInherit

Source

fn init_render_once(&mut self) -> BlockInherit

Source

fn ends_render(&mut self)

Source

fn set_status_code(&mut self)

Source

fn set_moveto(&mut self)

Source

fn replacements(&mut self)

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.
Source

pub fn get_time_duration(&self) -> Duration

Retrieves the time duration for template rendering.

§Returns

The time duration elapsed .

Auto Trait Implementations§

§

impl<'a> Freeze for Template<'a>

§

impl<'a> RefUnwindSafe for Template<'a>

§

impl<'a> Send for Template<'a>

§

impl<'a> Sync for Template<'a>

§

impl<'a> Unpin for Template<'a>

§

impl<'a> UnwindSafe for Template<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Ungil for T
where T: Send,