Skip to main content

LuaEnvironment

Struct LuaEnvironment 

Source
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>>§

Source

pub fn keep_trailing_newline(&self) -> bool

Returns the value of the trailing newline preservation flag.

Source

pub fn trim_blocks(&self) -> bool

Returns the value of the trim blocks flag.

Source

pub fn lstrip_blocks(&self) -> bool

Returns the value of the lstrip blocks flag.

Source

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());
}
Source

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());
Source

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());
Source

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

Source

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.

Source

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.

Source

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.

Source

pub fn debug(&self) -> bool

Returns the current value of the debug flag.

Source

pub fn fuel(&self) -> Option<u64>

Returns the configured fuel.

Source

pub fn syntax(&self) -> &SyntaxConfig

Returns the current syntax config.

Source

pub fn recursion_limit(&self) -> usize

Returns the current max recursion limit.

Source

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.

Source

pub fn compile_expression_owned<E>( &self, expr: E, ) -> Result<Expression<'_, 'source>, Error>
where E: Into<Cow<'source, str>>,

Compiles an expression without capturing the lifetime.

This works exactly like compile_expression but lets you pass an owned string without capturing the lifetime.

Source

pub fn globals(&self) -> impl Iterator<Item = (&str, Value)>

Returns an iterator of all global variables.

Source

pub fn empty_state(&self) -> State<'_, '_>

Returns an empty State for testing purposes and similar.

Trait Implementations§

Source§

impl Debug for LuaEnvironment

Source§

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

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

impl Deref for LuaEnvironment

Source§

type Target = Environment<'static>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Display for LuaEnvironment

Source§

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

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

impl From<Environment<'static>> for LuaEnvironment

Source§

fn from(value: Environment<'static>) -> Self

Converts to this type from the input type.
Source§

impl From<LuaEnvironment> for Environment<'static>

Source§

fn from(value: LuaEnvironment) -> Self

Converts to this type from the input type.
Source§

impl UserData for LuaEnvironment

Source§

fn register(registry: &mut UserDataRegistry<Self>)

Registers this type for use in Lua. Read more
Source§

fn add_fields<F>(fields: &mut F)
where F: UserDataFields<Self>,

Adds custom fields specific to this userdata.
Source§

fn add_methods<M>(methods: &mut M)
where M: UserDataMethods<Self>,

Adds custom methods and operators specific to this userdata.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> IntoLua for T
where T: UserData + MaybeSend + MaybeSync + 'static,

Source§

fn into_lua(self, lua: &Lua) -> Result<Value, Error>

Performs the conversion.
Source§

impl<T> IntoLuaMulti for T
where T: IntoLua,

Source§

fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue, Error>

Performs the conversion.
Source§

unsafe fn push_into_stack_multi(self, lua: &RawLua) -> Result<i32, Error>

Source§

impl<T> MaybeSend for T

Source§

impl<T> MaybeSync for T

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.