pub struct Atom { /* private fields */ }Expand description
The main template engine struct.
Atom provides methods for creating templates, registering components, rendering templates, and managing context.
§Example
use atom_engine::Atom;
use serde_json::json;
let mut engine = Atom::new();
engine.add_template("greeting.html", "Hello, {{ name }}!").unwrap();
let result = engine.render("greeting.html", &json!({"name": "Alice"})).unwrap();
assert_eq!(result, "Hello, Alice!");Implementations§
Source§impl Atom
impl Atom
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new Atom engine instance with all built-in filters and functions registered.
§Example
use atom_engine::Atom;
let engine = Atom::new();Sourcepub fn register_filter<F>(&mut self, name: &str, filter: F)
pub fn register_filter<F>(&mut self, name: &str, filter: F)
Sourcepub fn register_function<F>(&mut self, name: &str, function: F)
pub fn register_function<F>(&mut self, name: &str, function: F)
Registers a custom global function.
§Arguments
name- Function namefunction- The function to register
Sourcepub fn set_max_loop_iter(&mut self, max: usize)
pub fn set_max_loop_iter(&mut self, max: usize)
Sets the maximum number of iterations for loops.
This prevents infinite loops in templates.
Sourcepub fn set_debug(&mut self, debug: bool)
pub fn set_debug(&mut self, debug: bool)
Sets debug mode for the engine.
When enabled, additional debugging information may be logged.
Sourcepub fn render(&self, template: &str, context: &Value) -> Result<String, Error>
pub fn render(&self, template: &str, context: &Value) -> Result<String, Error>
Renders a template with the given context.
§Arguments
template- The template name to rendercontext- The context data as a JSON value
§Example
use atom_engine::Atom;
use serde_json::json;
let mut engine = Atom::new();
engine.add_template("greeting.html", "Hello, {{ name }}!").unwrap();
let result = engine.render("greeting.html", &json!({"name": "World"})).unwrap();
assert_eq!(result, "Hello, World!");Sourcepub fn render_with_components(
&self,
template: &str,
context: &Value,
component_data: &Value,
) -> Result<String, Error>
pub fn render_with_components( &self, template: &str, context: &Value, component_data: &Value, ) -> Result<String, Error>
Renders a template with component data included in the context.
This is useful when you want to pass additional component-specific data alongside the regular context.
§Arguments
template- The template name to rendercontext- The context data as a JSON valuecomponent_data- Additional component-specific data
Sourcepub fn provide(&mut self, key: &str, value: Value)
pub fn provide(&mut self, key: &str, value: Value)
Provides a value to the context chain.
This implements a provide/inject pattern (similar to Vue.js) where values can be provided at a higher level and injected in child components.
§Arguments
key- The context keyvalue- The value to provide
§Example
use atom_engine::Atom;
use serde_json::json;
let mut engine = Atom::new();
engine.add_template("child.html", "{{ theme }}").unwrap();
engine.provide("theme", json!("dark"));
let result = engine.render("child.html", &json!({})).unwrap();
assert_eq!(result, "dark");Sourcepub fn reload(&mut self) -> Result<(), Error>
pub fn reload(&mut self) -> Result<(), Error>
Reloads all templates from the filesystem.
This is useful during development when templates change on disk.
Sourcepub fn template_exists(&self, name: &str) -> bool
pub fn template_exists(&self, name: &str) -> bool
Sourcepub fn get_registered_templates(&self) -> Vec<String>
pub fn get_registered_templates(&self) -> Vec<String>
Sourcepub fn clear_cache(&mut self)
pub fn clear_cache(&mut self)
Clears the template cache.
This forces templates to be re-parsed on next render.
Sourcepub fn set_parallel(&mut self, enabled: bool)
pub fn set_parallel(&mut self, enabled: bool)
Enables or disables parallel rendering.
When enabled with the parallel feature, multiple templates can be
rendered concurrently using Rayon.
Sourcepub fn is_parallel(&self) -> bool
pub fn is_parallel(&self) -> bool
Returns whether parallel rendering is enabled.
Sourcepub fn enable_component_cache(&mut self, enabled: bool)
pub fn enable_component_cache(&mut self, enabled: bool)
Enables or disables component caching.
When enabled, rendered components are cached based on their props hash.
Sourcepub fn is_component_cache_enabled(&self) -> bool
pub fn is_component_cache_enabled(&self) -> bool
Returns whether component caching is enabled.
Sourcepub fn clear_component_cache(&mut self)
pub fn clear_component_cache(&mut self)
Clears the component cache.
Sourcepub fn component_cache_len(&self) -> usize
pub fn component_cache_len(&self) -> usize
Returns the number of cached component renders.
Sourcepub fn render_many(
&self,
templates: &[(&str, &Value)],
) -> Result<Vec<(String, String)>, Error>
pub fn render_many( &self, templates: &[(&str, &Value)], ) -> Result<Vec<(String, String)>, Error>
Sourcepub async fn render_async(
&self,
template: &str,
context: &Value,
) -> Result<String, Error>
pub async fn render_async( &self, template: &str, context: &Value, ) -> Result<String, Error>
Renders a template asynchronously.
Requires the async feature to be enabled.
§Arguments
template- The template name to rendercontext- The context data as a JSON value
§Example
#[cfg(feature = "async")]
async fn render_template() {
use atom_engine::Atom;
use serde_json::json;
let engine = Atom::new();
let result = engine.render_async("hello.html", &json!({"name": "World"})).await;
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Atom
impl !RefUnwindSafe for Atom
impl Send for Atom
impl Sync for Atom
impl Unpin for Atom
impl UnsafeUnpin for Atom
impl !UnwindSafe for Atom
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more