pub struct LuaEnvironment(/* private fields */);Expand description
A wrapper around a minijinja::Environment. This wrapper can be serialized into
an mlua::UserData object for use within mlua::Lua.
Methods from Deref<Target = Environment<'static>>§
Sourcepub fn keep_trailing_newline(&self) -> bool
pub fn keep_trailing_newline(&self) -> bool
Returns the value of the trailing newline preservation flag.
Sourcepub fn trim_blocks(&self) -> bool
pub fn trim_blocks(&self) -> bool
Returns the value of the trim blocks flag.
Sourcepub fn lstrip_blocks(&self) -> bool
pub fn lstrip_blocks(&self) -> bool
Returns the value of the lstrip blocks flag.
Sourcepub fn templates(&self) -> impl Iterator<Item = (&str, Template<'_, '_>)>
pub fn templates(&self) -> impl Iterator<Item = (&str, Template<'_, '_>)>
Returns an iterator over the already loaded templates and their names.
Only templates that are already loaded will be returned.
let mut env = Environment::new();
env.add_template("hello.txt", "Hello {{ name }}!").unwrap();
env.add_template("goodbye.txt", "Goodbye {{ name }}!").unwrap();
for (name, tmpl) in env.templates() {
println!("{}", tmpl.render(context!{ name => "World" }).unwrap());
}Sourcepub fn get_template(&self, name: &str) -> Result<Template<'_, '_>, Error>
pub fn get_template(&self, name: &str) -> Result<Template<'_, '_>, Error>
Fetches a template by name.
This requires that the template has been loaded with
add_template beforehand. If the template was
not loaded an error of kind TemplateNotFound is returned. If a loader was
added to the engine this can also dynamically load templates.
let mut env = Environment::new();
env.add_template("hello.txt", "Hello {{ name }}!").unwrap();
let tmpl = env.get_template("hello.txt").unwrap();
println!("{}", tmpl.render(context!{ name => "World" }).unwrap());Sourcepub fn template_from_named_str(
&self,
name: &'source str,
source: &'source str,
) -> Result<Template<'_, 'source>, Error>
pub fn template_from_named_str( &self, name: &'source str, source: &'source str, ) -> Result<Template<'_, 'source>, Error>
Loads a template from a string.
In some cases you only need to work with (e.g., render) a template once.
let env = Environment::new();
let tmpl = env.template_from_named_str("template_name", "Hello {{ name }}").unwrap();
let rv = tmpl.render(context! { name => "World" });
println!("{}", rv.unwrap());Sourcepub fn template_from_str(
&self,
source: &'source str,
) -> Result<Template<'_, 'source>, Error>
pub fn template_from_str( &self, source: &'source str, ) -> Result<Template<'_, 'source>, Error>
Loads a template from a string, with name <string>.
This is a shortcut to template_from_named_str
with name set to <string>.
Sourcepub fn render_named_str<S>(
&self,
name: &str,
source: &str,
ctx: S,
) -> Result<String, Error>where
S: Serialize,
pub fn render_named_str<S>(
&self,
name: &str,
source: &str,
ctx: S,
) -> Result<String, Error>where
S: Serialize,
Parses and renders a template from a string in one go with name.
Like render_str, but provide a name for the
template to be used instead of the default <string>. This is an
alias for template_from_named_str paired with
render.
let env = Environment::new();
let rv = env.render_named_str(
"template_name",
"Hello {{ name }}",
context!{ name => "World" }
);
println!("{}", rv.unwrap());Note on values: The Value type implements Serialize and can be
efficiently passed to render. It does not undergo actual serialization.
Sourcepub fn render_str<S>(&self, source: &str, ctx: S) -> Result<String, Error>where
S: Serialize,
pub fn render_str<S>(&self, source: &str, ctx: S) -> Result<String, Error>where
S: Serialize,
Parses and renders a template from a string in one go.
In some cases you really only need a template to be rendered once from
a string and returned. The internal name of the template is <string>.
This is an alias for template_from_str paired with
render.
Note on values: The Value type implements Serialize and can be
efficiently passed to render. It does not undergo actual serialization.
Sourcepub fn undefined_behavior(&self) -> UndefinedBehavior
pub fn undefined_behavior(&self) -> UndefinedBehavior
Returns the current undefined behavior.
This is particularly useful if a filter function or similar wants to change its behavior with regards to undefined values.
Sourcepub fn syntax(&self) -> &SyntaxConfig
pub fn syntax(&self) -> &SyntaxConfig
Returns the current syntax config.
Sourcepub fn recursion_limit(&self) -> usize
pub fn recursion_limit(&self) -> usize
Returns the current max recursion limit.
Sourcepub fn compile_expression(
&self,
expr: &'source str,
) -> Result<Expression<'_, 'source>, Error>
pub fn compile_expression( &self, expr: &'source str, ) -> Result<Expression<'_, 'source>, Error>
Compiles an expression.
This lets you compile an expression in the template language and evaluate it.
This makes it possible to use the language’s expressions as a minimal
scripting language. For more information and an
example see Expression.
Sourcepub fn compile_expression_owned<E>(
&self,
expr: E,
) -> Result<Expression<'_, 'source>, Error>
pub fn compile_expression_owned<E>( &self, expr: E, ) -> Result<Expression<'_, 'source>, Error>
Compiles an expression without capturing the lifetime.
This works exactly like compile_expression but
lets you pass an owned string without capturing the lifetime.
Sourcepub fn globals(&self) -> impl Iterator<Item = (&str, Value)>
pub fn globals(&self) -> impl Iterator<Item = (&str, Value)>
Returns an iterator of all global variables.
Sourcepub fn empty_state(&self) -> State<'_, '_>
pub fn empty_state(&self) -> State<'_, '_>
Returns an empty State for testing purposes and similar.
Trait Implementations§
Source§impl Debug for LuaEnvironment
impl Debug for LuaEnvironment
Source§impl Deref for LuaEnvironment
impl Deref for LuaEnvironment
Source§impl Display for LuaEnvironment
impl Display for LuaEnvironment
Source§impl From<Environment<'static>> for LuaEnvironment
impl From<Environment<'static>> for LuaEnvironment
Source§fn from(value: Environment<'static>) -> Self
fn from(value: Environment<'static>) -> Self
Source§impl From<LuaEnvironment> for Environment<'static>
impl From<LuaEnvironment> for Environment<'static>
Source§fn from(value: LuaEnvironment) -> Self
fn from(value: LuaEnvironment) -> Self
Source§impl UserData for LuaEnvironment
impl UserData for LuaEnvironment
Source§fn register(registry: &mut UserDataRegistry<Self>)
fn register(registry: &mut UserDataRegistry<Self>)
Source§fn add_fields<F>(fields: &mut F)where
F: UserDataFields<Self>,
fn add_fields<F>(fields: &mut F)where
F: UserDataFields<Self>,
Source§fn add_methods<M>(methods: &mut M)where
M: UserDataMethods<Self>,
fn add_methods<M>(methods: &mut M)where
M: UserDataMethods<Self>,
Auto Trait Implementations§
impl !Freeze for LuaEnvironment
impl !RefUnwindSafe for LuaEnvironment
impl !UnwindSafe for LuaEnvironment
impl Send for LuaEnvironment
impl Sync for LuaEnvironment
impl Unpin for LuaEnvironment
impl UnsafeUnpin for LuaEnvironment
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
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>
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>
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