Struct reinda_core::template::Template
source · 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
impl Template
sourcepub fn parse(input: Bytes) -> Result<Self, Error>
pub fn parse(input: Bytes) -> Result<Self, Error>
Parses the input byte string as template. Returns Err
on parse error.
sourcepub fn into_already_rendered(self) -> Result<Bytes, Self>
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
.
sourcepub fn fragments(&self) -> impl Iterator<Item = &Fragment>
pub fn fragments(&self) -> impl Iterator<Item = &Fragment>
Returns an iterator over all fragments.
sourcepub fn render<R, E>(self, replacer: R) -> Result<Bytes, E>where
R: FnMut(Fragment, Appender<'_>) -> Result<(), E>,
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.