pub struct Template { /* private fields */ }Expand description
A simple template engine for prompt templates.
Templates use {{VARIABLE}} syntax for placeholders and {{> partial}} for
including shared templates. Variables are replaced with the provided values.
Comments using {# comment #} syntax are stripped.
§Example
ⓘ
let partials = HashMap::from([("header", "Common Header\n")]);
let template = Template::new("{{> header}}\nReview this diff:\n{{DIFF}}");
let variables = HashMap::from([("DIFF", "+ new line")]);
let rendered = template.render_with_partials(&variables, &partials)?;Implementations§
Source§impl Template
impl Template
Sourcepub fn new(content: &str) -> Self
pub fn new(content: &str) -> Self
Create a template from a string.
Comments ({# ... #}) are stripped during creation.
All features are enabled by default: variables, conditionals, loops, and defaults.
Sourcepub fn render(
&self,
variables: &HashMap<&str, String>,
) -> Result<String, TemplateError>
pub fn render( &self, variables: &HashMap<&str, String>, ) -> Result<String, TemplateError>
Render the template with the provided variables.
Sourcepub fn render_with_partials(
&self,
variables: &HashMap<&str, String>,
partials: &HashMap<String, String>,
) -> Result<String, TemplateError>
pub fn render_with_partials( &self, variables: &HashMap<&str, String>, partials: &HashMap<String, String>, ) -> Result<String, TemplateError>
Render the template with variables and partials support.
Partials are processed recursively, with the same variables passed to each partial. Circular references are detected and reported with a clear error.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Template
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnwindSafe for Template
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
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>
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 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>
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