Struct minijinja::Template

source ·
pub struct Template<'env> { /* private fields */ }
Expand description

Represents a handle to a template.

Templates are stored in the Environment as bytecode instructions. With the Environment::get_template method that is looked up and returned in form of this handle. Such a template can be cheaply copied as it only holds references.

To render the render method can be used.

Implementations§

source§

impl<'env> Template<'env>

source

pub fn name(&self) -> &str

Returns the name of the template.

source

pub fn source(&self) -> &str

Returns the source code of the template.

source

pub fn render<S: Serialize>(&self, ctx: S) -> Result<String, Error>

Renders the template into a string.

The provided value is used as the initial context for the template. It can be any object that implements Serialize. You can either create your own struct and derive Serialize for it or the context! macro can be used to create an ad-hoc context.

let tmpl = env.get_template("hello").unwrap();
println!("{}", tmpl.render(context!(name => "John")).unwrap());

Note on values: The Value type implements Serialize and can be efficiently passed to render. It does not undergo actual serialization.

source

pub fn render_block<S: Serialize>( &self, block: &str, ctx: S ) -> Result<String, Error>

Renders the template block into a string.

This method works like render but it only renders a specific block in the template. The first argument is the name of the block.

This renders only the block hi in the template:

let tmpl = env.get_template("hello").unwrap();
println!("{}", tmpl.render_block("hi", context!(name => "John")).unwrap());

Note on values: The Value type implements Serialize and can be efficiently passed to render. It does not undergo actual serialization.

source

pub fn render_to_write<S: Serialize, W: Write>( &self, ctx: S, w: W ) -> Result<(), Error>

Renders the template into a io::Write.

This works exactly like render but instead writes the template as it’s evaluating into a io::Write.

This renders only the block hi in the template:

use std::io::stdout;

let tmpl = env.get_template("hello").unwrap();
tmpl.render_to_write(context!(name => "John"), &mut stdout()).unwrap();

Note on values: The Value type implements Serialize and can be efficiently passed to render. It does not undergo actual serialization.

source

pub fn render_block_to_write<S: Serialize, W: Write>( &self, block: &str, ctx: S, w: W ) -> Result<(), Error>

Renders the template block into a io::Write.

This works exactly like render_to_write but renders a single block. The first argument is the name of the block.

use std::io::stdout;

let tmpl = env.get_template("hello").unwrap();
tmpl.render_block_to_write("hi", context!(name => "John"), &mut stdout()).unwrap();

Note on values: The Value type implements Serialize and can be efficiently passed to render. It does not undergo actual serialization.

source

pub fn undeclared_variables(&self, nested: bool) -> HashSet<String>

Returns a set of all undeclared variables in the template.

This returns a set of all variables that might be looked up at runtime by the template. Since this is runs a static analysis, the actual control flow is not considered. This also cannot take into account what happens due to includes, imports or extending. If nested is set to true, then also nested trivial attribute lookups are considered and returned.

let mut env = Environment::new();
env.add_template("x", "{% set x = foo %}{{ x }}{{ bar.baz }}").unwrap();
let tmpl = env.get_template("x").unwrap();
let undeclared = tmpl.undeclared_variables(false);
// returns ["foo", "bar"]
let undeclared = tmpl.undeclared_variables(true);
// returns ["foo", "bar.baz"]

Trait Implementations§

source§

impl<'env> Clone for Template<'env>

source§

fn clone(&self) -> Template<'env>

Returns a copy 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<'env> Debug for Template<'env>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'env> Copy for Template<'env>

Auto Trait Implementations§

§

impl<'env> !RefUnwindSafe for Template<'env>

§

impl<'env> Send for Template<'env>

§

impl<'env> Sync for Template<'env>

§

impl<'env> Unpin for Template<'env>

§

impl<'env> !UnwindSafe for Template<'env>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.