Skip to main content

Atom

Struct Atom 

Source
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

Source

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

pub fn load_templates(&mut self, glob_pattern: &str) -> Result<(), Error>

Loads templates from the filesystem using a glob pattern.

§Arguments
  • glob_pattern - A glob pattern to match template files (e.g., “templates/**/*.html”)
§Example
let mut engine = atom_engine::Atom::new();
engine.load_templates("templates/**/*.html").unwrap();
Source

pub fn add_template(&mut self, name: &str, content: &str) -> Result<(), Error>

Adds a raw template to the engine.

§Arguments
  • name - The template name (e.g., “index.html”)
  • content - The template content
§Example
let mut engine = atom_engine::Atom::new();
engine.add_template("hello.html", "Hello, {{ name }}!").unwrap();
Source

pub fn register_component( &mut self, path: &str, template: &str, ) -> Result<(), Error>

Registers a reusable component.

§Arguments
  • path - Component path/name (e.g., “button”)
  • template - Component template content
§Example
let mut engine = atom_engine::Atom::new();
engine.register_component("button", "<button>{{ text }}</button>").unwrap();
Source

pub fn register_filter<F>(&mut self, name: &str, filter: F)
where F: Filter + Send + Sync + 'static,

Registers a custom filter.

§Arguments
  • name - Filter name
  • filter - The filter function
Source

pub fn register_function<F>(&mut self, name: &str, function: F)
where F: Function + Send + Sync + 'static,

Registers a custom global function.

§Arguments
  • name - Function name
  • function - The function to register
Source

pub fn set_max_loop_iter(&mut self, max: usize)

Sets the maximum number of iterations for loops.

This prevents infinite loops in templates.

Source

pub fn set_debug(&mut self, debug: bool)

Sets debug mode for the engine.

When enabled, additional debugging information may be logged.

Source

pub fn render(&self, template: &str, context: &Value) -> Result<String, Error>

Renders a template with the given context.

§Arguments
  • template - The template name to render
  • context - 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!");
Source

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 render
  • context - The context data as a JSON value
  • component_data - Additional component-specific data
Source

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 key
  • value - 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");
Source

pub fn reload(&mut self) -> Result<(), Error>

Reloads all templates from the filesystem.

This is useful during development when templates change on disk.

Source

pub fn template_exists(&self, name: &str) -> bool

Checks if a template exists in the engine.

§Arguments
  • name - The template name to check
§Returns

true if the template exists, false otherwise

Source

pub fn get_registered_templates(&self) -> Vec<String>

Gets a list of all registered template names.

§Returns

A vector of template names

Source

pub fn clear_cache(&mut self)

Clears the template cache.

This forces templates to be re-parsed on next render.

Source

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.

Source

pub fn is_parallel(&self) -> bool

Returns whether parallel rendering is enabled.

Source

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.

Source

pub fn is_component_cache_enabled(&self) -> bool

Returns whether component caching is enabled.

Source

pub fn clear_component_cache(&mut self)

Clears the component cache.

Source

pub fn component_cache_len(&self) -> usize

Returns the number of cached component renders.

Source

pub fn render_many( &self, templates: &[(&str, &Value)], ) -> Result<Vec<(String, String)>, Error>

Renders multiple templates in parallel (when the parallel feature is enabled).

Requires the parallel feature to be enabled. Without it, templates are rendered sequentially.

§Arguments
  • templates - A slice of template name and context pairs
§Returns

A vector of (template_name, rendered_output) pairs

Source

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 render
  • context - 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;
}
Source

pub async fn render_many_async( &self, templates: &[(&str, &Value)], ) -> Result<Vec<(String, String)>, Error>

Renders multiple templates asynchronously.

Requires the async feature to be enabled.

§Arguments
  • templates - A slice of template name and context pairs
§Returns

A vector of (template_name, rendered_output) pairs

Trait Implementations§

Source§

impl Clone for Atom

Source§

fn clone(&self) -> Atom

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Atom

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V