pub struct Template { /* private fields */ }
Expand description

A parsed template.

Template syntax

Our template syntax is super simple and is really just a glorified search-and-replace. The input is checked for “fragments” which have the syntax {{: foo :}}. The start token is actually {{: (note the whitespace!). So {{:foo:}} is not recognized as fragment.

There are two additional constraints: the fragment must not contain a newline and must be shorter than MAX_FRAGMENT_LEN. If these conditions are not met, the fragment start token is ignored.

The string between the start and end tag is then trimmed (excess whitespace removed) and parsed into a Fragment. See that type’s documentation for information about the existing kinds of fragments.

Implementations§

source§

impl Template

source

pub fn parse(input: Bytes) -> Result<Self, Error>

Parses the input byte string as template. Returns Err on parse error.

source

pub fn into_already_rendered(self) -> Result<Bytes, Self>

Returns Some(out) if this template does not have any fragments at all. out is equal to the input that was passed to parse.

source

pub fn fragments(&self) -> impl Iterator<Item = &Fragment>

Returns an iterator over all fragments.

source

pub fn raw_input(&self) -> &Bytes

Returns the raw input that was passed to parse.

source

pub fn render<R, E>(self, replacer: R) -> Result<Bytes, E>where R: FnMut(Fragment, Appender<'_>) -> Result<(), E>,

Renders the template using replacer to evaluate the fragments.

Replacing/evaluating fragments

For each fragment in the input template, the replacer is called with the parsed fragment. For example, the template string foo {{: bar :}} baz {{: config.data :}}x would lead to two calls to replacer, with the following strings as first parameter:

  • bar
  • config.data

As you can see, excess whitespace is stripped before passing the string within the fragment.

Trait Implementations§

source§

impl Debug for Template

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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.