Module molde
Functions
parse (template) | Parse a template, returning a table with the matched contents. |
compile (template) | Compiles a table with contents to Lua code that generates the (hopefully) desired result. |
load (template[, chunkname='molde generator']) | Compiles the template, returning a closure that executes the substitution. |
loadfile (template_file) | Same as molde.load, but loads the template from a file. |
__process_template_function ([values[, env=_G]]) | This is the prototype of the function returned by molde.load and molde.loadfile. |
Tables
errors | Parse errors that can occur in a template |
Fields
VERSION | Module version 1.0.0 |
string_bracket_level | Long string bracket level. |
Functions
- parse (template)
-
Parse a template, returning a table with the matched contents.
The parser tags the contents as:
- Literal: text that should be just copied to result
- Value: a value to be substituted using Lua, usually a variable; it will be stringified using tostring
- Statement: one or more Lua statements that will be copied directly into the compiled function
Results are in the format
{['literal' | 'value' | 'statement'] = <captured value>}
Parameters:
- template Template string to be parsed
Returns:
-
Table with results
Or
-
nil
- Parse error
Usage:
local results = molde.parse([[literal {{ value }} {% statement %}]]) --[[ -- results: { -- {literal = "literal "}, -- {value = " value "}, -- {literal = " "}, -- {statement = " statement "} -- } --]]
- compile (template)
-
Compiles a table with contents to Lua code that generates the (hopefully)
desired result.
The code generated for literals use Lua's long strings, with a default level of 1. This level is taken from molde.string_bracket_level, and can be changed if your literal may contain the string
"]=]"
, for example.Parameters:
- template Template string to be compiled
Returns:
-
string
Generated code
Or
-
nil
- Parse error
- load (template[, chunkname='molde generator'])
-
Compiles the template, returning a closure that executes the substitution.
The returned function behaves as described in __process_template_function.
Parameters:
- template Template string
- chunkname Name of the chunk for error messages (default 'molde generator')
Returns:
-
function
Template processor
Or
-
nil
- Parse error
Usage:
hello_template = molde.load([[Hello {{ name or "world" }}]]) print(hello_template()) -- "Hello world"
- loadfile (template_file)
-
Same as molde.load, but loads the template from a file.
Uses
template_file
as chunkname.Every caveat for molde.load applies.
Parameters:
- template_file Template file path
Returns:
-
function
Template processor
Or
-
nil
- File open error
Or
-
nil
- Parse error
See also:
Usage:
hello_template = molde.loadfile("hello_template_file") print(hello_template()) -- "Hello world"
- __process_template_function ([values[, env=_G]])
-
This is the prototype of the function returned by molde.load and
molde.loadfile.
The environment is sandboxed, and assigning variables directly to it won't affect the original tables. Variable lookup order: local environment,
values
,env
. Theenv
table serves as a fallback environment, and is useful when you want to sandbox builtin Lua functions.Any non-local variables assigned in the template are stored in the sandboxed environment, which is the function's environment (
_ENV or getfenv()
).Parameters:
- values Table with the values to substitute (optional)
- env Fallback environment (default _G)
Returns:
Raises:
When the generated code is invalid
Tables
- errors
-
Parse errors that can occur in a template
Fields:
- PegError When PEG couldn't parse
- ExpectedClosingValueError
There is no closing
"}}"
to a value - ExpectedClosingStatementError
There is no closing
"%}"
to a statement - UnexpectedClosingValueError
There is a closing
"}}"
without the corresponding"{{"
- UnexpectedClosingStatementError
There is a closing
"%}"
without the corresponding"{%"
- EmptyValueError
There is no content after value opening
"{{"
- EmptyStatementError
There is no content after statement opening
"{%"