pub struct TemplateBuilder { /* private fields */ }Expand description
A builder for creating and rendering templates using the Tera template engine.
Implementations§
Source§impl TemplateBuilder
impl TemplateBuilder
Sourcepub fn add_var<S, V>(self, name: S, value: V) -> Self
pub fn add_var<S, V>(self, name: S, value: V) -> Self
Adds a variable to the template context.
§Arguments
name- The name of the variable to addvalue- The value to associate with the variable
§Returns
The builder instance for method chaining
§Example
use sal_text::TemplateBuilder;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let builder = TemplateBuilder::open("templates/example.html")?
.add_var("title", "Hello World")
.add_var("username", "John Doe");
Ok(())
}Sourcepub fn add_vars<S, V>(self, vars: HashMap<S, V>) -> Self
pub fn add_vars<S, V>(self, vars: HashMap<S, V>) -> Self
Adds multiple variables to the template context from a HashMap.
§Arguments
vars- A HashMap containing variable names and values
§Returns
The builder instance for method chaining
§Example
use sal_text::TemplateBuilder;
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut vars = HashMap::new();
vars.insert("title", "Hello World");
vars.insert("username", "John Doe");
let builder = TemplateBuilder::open("templates/example.html")?
.add_vars(vars);
Ok(())
}Sourcepub fn render(&mut self) -> Result<String, Error>
pub fn render(&mut self) -> Result<String, Error>
Renders the template with the current context.
§Returns
The rendered template as a string
§Example
use sal_text::TemplateBuilder;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let result = TemplateBuilder::open("templates/example.html")?
.add_var("title", "Hello World")
.add_var("username", "John Doe")
.render()?;
println!("Rendered template: {}", result);
Ok(())
}Sourcepub fn render_to_file<P: AsRef<Path>>(&mut self, output_path: P) -> Result<()>
pub fn render_to_file<P: AsRef<Path>>(&mut self, output_path: P) -> Result<()>
Renders the template and writes the result to a file.
§Arguments
output_path- The path where the rendered template should be written
§Returns
Result indicating success or failure
§Example
use sal_text::TemplateBuilder;
fn main() -> Result<(), Box<dyn std::error::Error>> {
TemplateBuilder::open("templates/example.html")?
.add_var("title", "Hello World")
.add_var("username", "John Doe")
.render_to_file("output.html")?;
Ok(())
}Trait Implementations§
Source§impl Clone for TemplateBuilder
impl Clone for TemplateBuilder
Source§fn clone(&self) -> TemplateBuilder
fn clone(&self) -> TemplateBuilder
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for TemplateBuilder
impl !RefUnwindSafe for TemplateBuilder
impl Send for TemplateBuilder
impl Sync for TemplateBuilder
impl Unpin for TemplateBuilder
impl UnsafeUnpin for TemplateBuilder
impl !UnwindSafe for TemplateBuilder
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
Mutably borrows from an owned value. Read more