Module minijinja

Require: require("minijinja")

Index

Function

path_loader()

Get a callback to load templates from the provided directory paths.

type()

Get the type of value

Class

Environment

A minijinja environment.

State

A minijinja state.

SyntaxConfig

Configure the syntax for the environment.

Alias

AutoEscape

Determines how autoescaping is applied.

AutoEscapeCallback

A callback to select the default auto escaping.

Callback

A minijinja callback.

CallbackStateless

A stateless minijinja callback.

Filter

A minijinja filter function.

FormatterCallback

A callback to control how values are formatted.

Global

A minijinja global variable.

LoaderCallback

A template loader callback.

None

This value can be used in place of nil to indicate intentionally null values.

PathJoinCallback

A path join callback

Test

A minijinja test function.

Types

MiniJinja types

UndefinedBehavior

Determines how undefined values are handled.

UnknownMethodCallback

A callback invoked for unknown methods on objects.

Api reference

alias minijinja.Types = ("environment" | "state" | "none")

MiniJinja types

alias minijinja.UndefinedBehavior = ("lenient" | "chainable" | "semi-strict" | "strict")

Determines how undefined values are handled.

Can be provided to Environment.undefined_behavior.

Variants:

  • lenient:

    • printing: empty string

    • iteration: empty array

    • attributes: fails

    • test: falsey

  • chainable:

    • printing: empty string

    • iteration: empty array

    • attributes: undefined

    • test: falsey

  • semi-strict:

    • printing: fails

    • iteration: fails

    • attributes: fails

    • test: falsey

  • strict:

    • printing: fails

    • iteration: fails

    • attributes: fails

    • test: fails

alias minijinja.AutoEscape = ("html" | "json" | "none")

Determines how autoescaping is applied.

Variants:

  • html

  • json

  • none

alias minijinja.Callback = fun(state: minijinja.State, ..., kwargs: table?): any

A minijinja callback.

It takes a State as the first paramter followed by any number of args.

If the last argument is a table, it will be interpreted as keyword arguments passed to the callback.

alias minijinja.CallbackStateless = fun(..., kwargs: table?): any

A stateless minijinja callback.

Similar to a Callback, but it is not passed a State.

If the last argument is a table, it will be interpreted as keyword arguments passed to the callback.

alias minijinja.Global = (any | minijinja.Callback | minijinja.CallbackStateless)

A minijinja global variable.

This type can be provided to Environment:add_global()

alias minijinja.Filter = (minijinja.Callback | minijinja.CallbackStateless)

A minijinja filter function.

This type of function can be provided to Environment:add_filter()

alias minijinja.Test = (minijinja.Callback | minijinja.CallbackStateless)

A minijinja test function.

This type of function can be provided to Environment:add_test()

alias minijinja.LoaderCallback = fun(name: string): string?

A template loader callback.

It takes the name of a template and returns the source or nil if no template could be found.

This type of function can be provided to Environment:set_loader() to load templates from a filesystem.

alias minijinja.PathJoinCallback = fun(name: string, parent: string): string

A path join callback

It takes the name of a template and the parent path and returns a new derived path.

This type of function can be provided to Environment:set_path_join_callback() to implement relative path resolution between templates.

alias minijinja.UnknownMethodCallback = fun(state: minijinja.State, value: any, method: string, args: any[]): any

A callback invoked for unknown methods on objects.

It takes a State, the object which the method was called on, the name of the method, and any arguments passed and returns any value.

This type of function can be provided to Environment:set_unknown_method_callback() to implement compatibility with python methods.

alias minijinja.AutoEscapeCallback = fun(name: string): minijinja.AutoEscape

A callback to select the default auto escaping.

It takes the name of a template and returns an AutoEscape variant.

This type of function can be provided to Environment:set_auto_escape_callback().

alias minijinja.FormatterCallback = fun(state: minijinja.State, value: any): string

A callback to control how values are formatted.

It takes a State and a value to be formatted, and it returns the formatted value as a string.

This type of function can be provided to Environment:set_formatter().

alias minijinja.None = lightuserdata

This value can be used in place of nil to indicate intentionally null values.

It maps to the minijinja::value::ValueKind::None variant.

class minijinja.SyntaxConfig

Configure the syntax for the environment.

block_delimiters: (string, string)?

Start and end delimiters

variable_delimiters: (string, string)?

Start and end delimiters

comment_delimiters: (string, string)?

Start and end delimiters

line_statement_prefix: string?
line_comment_prefix: string?
class minijinja.Environment: userdata

A minijinja environment.

new(self): minijinja.Environment

Create a new environment.

reload_before_render: boolean

Reload templates before each render.

keep_trailing_newline: boolean

Preserve trailing newlines at the end of templates.

trim_blocks: boolean

Remove the first newline after a block.

lstrip_blocks: boolean

Remove leading spaces and tabs from the start of a line to a block.

debug: boolean

Enable debug behavior.

fuel: number?

Sets the fuel of the engine. If nil, fuel usage is disabled.

recursion_limit: number

Reconfigures the runtime recursion limit. Default is 500.

undefined_behavior: minijinja.UndefinedBehavior

Changes the undefined behavior. Default is lenient.

empty(self): minijinja.Environment

Create an empty environment.

This environment has no default filters, tests, or globals.

add_template(self, name: string, source: string)

Add a template.

Parameters:
  • name (string) – The name of the template.

  • source (string) – The template source contents.

remove_template(self, name: string)

Remove a template.

Parameters:

name (string) – The name of the template.

clear_templates(self)

Remove all templates.

undeclared_variables(self, name: string, nested?: boolean): table

Return a table of all undeclared variables in a template.

Parameters:
  • name (string) – The name of the template.

  • nested? (boolean) – If true, nested trivial attribute lookups are also returned.

set_loader(self, callback: minijinja.LoaderCallback)

Sets a callback to load template sources.

set_path_join_callback(self, callback: minijinja.PathJoinCallback)

Sets a callback to join template paths.

set_unknown_method_callback(self, callback: minijinja.UnknownMethodCallback)

Sets a callback invoked for unknown methods on objects.

set_pycompat(self, enable?: boolean)

Enable python compatibility for object methods.

This sets Environment:set_unknown_method_callback() with a callback that enables some python object methods to increase compatibility with Jinja templates.

See: https://docs.rs/minijinja-contrib/latest/minijinja_contrib/pycompat/fn.unknown_method_callback.html

set_auto_escape_callback(self, callback: minijinja.AutoEscapeCallback)

Sets a callback to select the default auto escaping behavior.

set_formatter(self, callback: minijinja.FormatterCallback)

Sets a callback to control how values are formatted.

set_syntax(self, syntax: minijinja.SyntaxConfig)

Sets the syntax for the environment.

render_template(self, name: string, ctx?: table): string

Render a template.

Parameters:
  • name (string) – The name of the template to render.

  • ctx? (table) – The template context.

Returns:

_1 (string) – The rendered template.

render_str(self, source: string, ctx?: table, name?: string): string

Render a string directly.

Parameters:
  • source (string) – The template source.

  • ctx? (table) – The template context.

  • name? (string) – The name of the template. Defaults to <string>.

Returns:

_1 (string) – The rendered template.

eval(self, source: string, ctx?: table): any

Evaluate an expression.

Parameters:
  • source (string) – The expression source

  • ctx? (table) – The expression context.

Returns:

_1 (any) – The result of the expression

add_filter(
    self,
    name: string,
    filter: minijinja.Filter,
    pass_state?: boolean
)

Add a filter.

Parameters:
  • name (string) – The name of the filter.

  • filter (minijinja.Filter) – The filter.

  • pass_state? (boolean) – Whether to pass a State as the first argument.

remove_filter(self, name: string)

Remove a filter.

Parameters:

name (string) – The name of the filter.

add_test(
    self,
    name: string,
    test: minijinja.Test,
    pass_state?: boolean
)

Add a test.

Parameters:
  • name (string) – The name of the test.

  • test (minijinja.Test) – The test.

  • pass_state? (boolean) – Whether to pass a State as the first argument.

remove_test(self, name: string)

Remove a test.

Parameters:

name (string) – The name of the test.

add_global(
    self,
    name: string,
    global: minijinja.Global,
    pass_state?: boolean
)

Add a global variable.

Parameters:
  • name (string) – The name of the variable.

  • global (minijinja.Global) – The variable.

  • pass_state? (boolean) – Whether to pass a State as the first argument to function variables.

remove_global(self, name: string)

Remove a global variable.

Parameters:

name (string) – The name of the variable.

globals(self): any[]

Get a list of all global variables.

class minijinja.State: userdata

A minijinja state.

Only accesible within filters, tests, and global functions.

name(self): string

Get the name of the current template.

Returns:

_1 (string) – The template name.

auto_escape(self): minijinja.AutoEscape

Get the current value of the auto escape flag.

Returns:

_1 (minijinja.AutoEscape) – The current auto escape flag.

undefined_behavior(self): minijinja.UndefinedBehavior

Get the current undefined behavior.

Returns:

_1 (minijinja.UndefinedBehavior) – The current undefined behavior.

current_block(self): string

Get the name of the innermost block.

Returns:

_1 (string) – The name of the innermost block.

lookup(self, name: string): any

Look up a variable in the render context by name.

Parameters:

name (string) – The name of the variable.

Returns:

_1 (any) – The variable associated with name.

call_macro(self, name: string, ...: any): string

Call a macro.

Parameters:
  • name (string) – The name of the macro.

  • ... (any) – Arguments to pass to the macro.

Returns:

_1 (string) – The macro output.

exports(self): string[]

Get a list of names for all exports (top-level variables).

known_variables(self): string[]

Get a list of all known variables.

apply_filter(self, filter: string, ...: any): any

Invokes a filter with some arguments.

Parameters:
  • filter (string) – The name of the filter.

  • ... (any) – Arguments to pass to the filter.

Returns:

_1 (any) – The output of the filter.

perform_test(self, test: string, ...: any): boolean

Invokes a test function on a value.

Parameters:
  • test (string) – The name of the test.

  • ... (any) – Arguments to pass to the test.

Returns:

_1 (boolean) – The output of the test.

format(self, value: any): string

Format a value to a string using the formatter configured for the environment.

Parameters:

value (any) – The value to format.

Returns:

_1 (string) – The formatted value.

fuel_levels(self): (number, number)

Get the consumed and remaining fuel levels.

Returns:

_1 ((number, number)) – The [consumed, remaining] fuel levels.

get_temp(self, name: string): any

Look up a temp variable.

Parameters:

name (string) – The name of the variable.

Returns:

_1 (any) – The variable associated with name.

set_temp(self, name: string, temp: any): any

Set a temp variable and return the old value.

Parameters:
  • name (string) – The name of the variable.

  • temp (any) – The temp variable.

Returns:

_1 (any) – The old temp variable value.

get_or_set_temp(self, name: string, func: fun(): any): any

Get a temp variable or add the variable returned by func.

Parameters:
  • name (string) – The name of the variable.

  • func (fun(): any) – The function to call if the temp is not set.

Returns:

_1 (any) – The variable associated with name, or the variable returnd by func.

minijinja.type(value: any): (minijinja.Types | string)

Get the type of value

This function returns the strings

  • 'environment' for Environment

  • 'state' for State

  • 'none' for None

  • or the values returned by the builtin type() function.

minijinja.path_loader(paths: (string | string[])): minijinja.LoaderCallback

Get a callback to load templates from the provided directory paths.

The function returned by this one can be passed to Environment:set_loader() to load templates from the filesystem.