class Crinja
- Crinja
- Reference
- Object
Overview
This class represents the core component of the Crinja template engine.
It contains the runtime environment including configuration, global variables as well as loading and rendering templates.
Instances of this class may be modified if they are not shared and if no template was loaded so far. Modifications on environments after the first template was loaded will lead to surprising effects and undefined behavior.
It also contains macros to easily define custom template features such as filters, tests and functions.
Included Modules
Defined in:
crinja.crenvironment.cr
object.cr
runtime/callable.cr:1
runtime/callable.cr:99
runtime/safe_string.cr
runtime/undefined.cr
runtime/value.cr:1
runtime/value.cr:11
visitor/html.cr
visitor/inspector.cr
Constant Summary
-
Log =
::Log.for(self)
-
UNDEFINED =
Undefined.new("UNDEFINED")
-
VERSION =
{{ (`shards version /root/project/src`).chomp.stringify }}
Constructors
-
.new(context = Context.new, config = Config.new, loader = Loader::FileSystemLoader.new, cache = TemplateCache::InMemory.new, &)
Creates a new environment and yields
self
for configuration. -
.new(original : Crinja)
Creates a new environment with the context and configuration from the original environment.
-
.new(config : Config, loader = Loader::FileSystemLoader.new, cache = TemplateCache::InMemory.new)
Creates a new environment from config.
-
.new(context : Crinja::Context = Context.new, config : Crinja::Config = Config.new, loader : Crinja::Loader = Loader::FileSystemLoader.new, cache : Crinja::TemplateCache = TemplateCache::InMemory.new)
Creates a new environment with default values.
Class Method Summary
-
.dictionary(object) : Dictionary
Casts an object with hash-like interface to
Dictionary
. -
.render(io : IO, template, variables = nil, loader = nil, config = nil)
Render Crinja template template to an
IO
io. -
.render(template, variables = nil, loader = nil, config = nil) : String
Render Crinja template template to a String.
- .value(value) : Value
-
.variables(object) : Variables
Casts an object with hash-like interface to
Variables
, which can be used for name lookup.
Instance Method Summary
-
#cache : TemplateCache
A cache where parsed templates are stored.
-
#cache=(cache : TemplateCache)
A cache where parsed templates are stored.
-
#config : Config
The configuration for this environment.
-
#context : Context
The current context in which evaluation happens.
- #errors : Array(Exception)
- #errors=(errors : Array(Exception))
-
#evaluate(expression : AST::ExpressionNode, bindings) : Value
Evaluates a Crinja expression with
#evaluator
and returns the resulting raw value. - #evaluate(expression : AST::ExpressionNode) : Value
-
#evaluate(expression, bindings = nil) : String
Parses and evaluates a Crinja expression with
#evaluator
. -
#evaluator
Returns an
Crinja::Evaluator
which allows evaluation of expressions. -
#filters : Crinja::Filter::Library
Filter library for this environment.
-
#from_string(string : String)
Loads a template from string. This parses the given string and returns a
Template
object. -
#functions : Crinja::Function::Library
Function library for this environment.
-
#get_template(name : String, parent = nil, globals = nil)
Loads a template from the loader.
-
#get_template(names : Iterable(String), parent = nil, globals = nil)
Works like
#get_template(String)
but tries a number of templates before it fails. -
#global_context : Context
The global context.
-
#loader : Loader
The loader through which
#get_template
loads a template. -
#loader=(loader : Loader)
The loader through which
#get_template
loads a template. -
#logger : ::Log
The logger for this environment.
-
#operators : Crinja::Operator::Library
Operator library for this environment.
-
#policies : Hash(String, Crinja::Value)
Policies for this environment.
-
#select_template(names, parent = nil, globals = nil)
Alias for
#get_template(Iterable(String))
. -
#stringify(object, pretty = false)
Turns object into a string represenation using
Crinja::Finalizer
. -
#tags : Crinja::Tag::Library
Tag library for this environment.
-
#tests : Crinja::Test::Library
Test library for this environment.
-
#undefined(name = nil)
Creates a new
#undefined
. -
#with_scope(ctx : Context, &)
Executes the block inside the context
ctx
and returns to the previous context afterwards. -
#with_scope(bindings = nil, &)
Executes the block inside a new sub-context with optional local scoped bindings.
Macro Summary
-
filter(defaults = nil, name = nil, &block)
This macro returns a
Crinja::Callable
proc which implements aCrinja::Filter
. -
function(defaults = nil, name = nil, &block)
This macro returns a
Crinja::Callable
proc which implements aCrinja::Function
. -
test(defaults = nil, name = nil, &block)
This macro returns a
Crinja::Callable
proc which implements aCrinja::Test
.
Instance methods inherited from module Crinja::Resolver
call_filter(name : String, target : Value, varargs = [] of Value, kwargs = Variables.new) : Valuecall_filter(name : String, target) : Value call_filter, execute_call(callable, varargs : Array(Value) = [] of Value, kwargs : Variables = Variables.new, target : Value? = nil) : Value
execute_call(callable : Callable | Callable::Proc, arguments : Arguments) : Value execute_call, resolve(name : String) : Value resolve, resolve_callable(identifier) : Value resolve_callable, resolve_callable!(identifier : Callable | Callable::Proc) : Callable | Callable::Proc
resolve_callable!(identifier : Value) : Callable | Callable::Proc resolve_callable!
Class methods inherited from module Crinja::Resolver
resolve_attribute(name, object : Value) : Valueresolve_attribute(name, value) : Value resolve_attribute, resolve_dig(name : String, object) : Value
resolve_dig(name : Value, value : Value) : Value
resolve_dig(name, object) : Value resolve_dig, resolve_getattr(name : Value, value : Value) : Value
resolve_getattr(name, value) : Value resolve_getattr, resolve_method(name, value : Value) : Callable | Callable::Proc?
resolve_method(name, object) : Callable | Callable::Proc? resolve_method, resolve_with_hash_accessor(name : Value, value : Value) : Value
resolve_with_hash_accessor(name, value : Value) : Value resolve_with_hash_accessor
Constructor Detail
Creates a new environment and yields self
for configuration.
Creates a new environment with the context and configuration from the original environment.
Creates a new environment from config.
Creates a new environment with default values. The context becomes both #global_context
as well as current ``#context`.
Class Method Detail
Render Crinja template template to an IO
io.
Variables for the template can be assigned as parameter variables.
This uses default loader and config unless these are provided as optional parameters.
A new Crinja
instance will be created for each invocation and it will
parse the template. To parse the same template once and invoke it multiple
times, it needs to be created directly (using Crinja#from_string
or
Template.new
) and stored in a variable.
Render Crinja template template to a String.
Variables for the template can be assigned as parameter variables.
This uses default loader and config unless these are provided as optional parameters.
A new Crinja
instance will be created for each invocation and it will
parse the template. To parse the same template once and invoke it multiple
times, it needs to be created directly (using Crinja#from_string
or
Template.new
) and stored in a variable.
Casts an object with hash-like interface to Variables
, which can be
used for name lookup.
Instance Method Detail
A cache where parsed templates are stored. Defaults to TemplateCache::InMemory
.
A cache where parsed templates are stored. Defaults to TemplateCache::InMemory
.
The current context in which evaluation happens. It can only be changed by #with_context
.
Evaluates a Crinja expression with #evaluator
and returns the resulting raw value.
Parses and evaluates a Crinja expression with #evaluator
. Returns a string which will be
auto-escaped if config.autoescape?
is true
.
Filter library for this environment.
See Crinja::Filter
for a list of builtin filters.
Loads a template from string. This parses the given string and returns a Template
object.
Function library for this environment.
See Crinja::Function
for a list of builtin functions.
Loads a template from the loader. If a loader is configured this method ask the loader for the template and returns a Template
.
If the parent parameter is not None, join_path() is called to get the real template name before loading.
TODO parent parameter is not implemented.
The globals parameter can be used to provide template wide globals. These variables are available in the context at render time.
If the template does not exist a TemplateNotFoundError
is raised.
Works like #get_template(String)
but tries a number of templates before it fails. If it cannot find any of the templates, it will raise a TemplateNotFoundError
.
The loader through which #get_template
loads a template.
Defaults to Loader::FileSystemLoader
with searchpath of the current working directory.
The loader through which #get_template
loads a template.
Defaults to Loader::FileSystemLoader
with searchpath of the current working directory.
Operator library for this environment.
See Crinja::Operator
for a list of builtin operators.
Alias for #get_template(Iterable(String))
.
Turns object into a string represenation using Crinja::Finalizer
.
Tag library for this environment.
See Crinja::Tag
for a list of builtin tags.
Test library for this environment.
See Crinja::Test
for a list of builtin tests.
Executes the block inside the context ctx
and returns to the previous context afterwards.
Executes the block inside a new sub-context with optional local scoped bindings. Returns to the previous context afterwards.
Macro Detail
This macro returns a Crinja::Callable
proc which implements a Crinja::Filter
.
defaults are set as default values in the Crinja::Arguments
object,
which a call to this proc receives.
If a name is provided, the created proc will automatically be registered as a default filter
at Crinja::Filter::Library
.
The macro takes a block which will be the main body for the proc. There, the following variables are available:
- arguments :
Crinja::Arguments
- Call arguments from the caller including defaults. - env :
Crinja
- The current environment. - target :
Crinja::Value
- The value which is to be filtered. Short cut forarguments.target
.
This macro returns a Crinja::Callable
proc which implements a Crinja::Function
.
defaults are set as default values in the Crinja::Arguments
object,
which a call to this proc receives.
If a name is provided, the created proc will automatically be registered as a default golbal
function at Crinja::Function::Library
.
The macro takes a block which will be the main body for the proc. There, the following variables are available:
- arguments :
Crinja::Arguments
- Call arguments from the caller including defaults. - env :
Crinja
- The current environment.
This macro returns a Crinja::Callable
proc which implements a Crinja::Test
.
defaults are set as default values in the Crinja::Arguments
object,
which a call to this proc receives.
If a name is provided, the created proc will automatically be registered as a default test
at Crinja::Test::Library
.
The macro takes a block which will be the main body for the proc. There, the following variables are available:
- arguments :
Crinja::Arguments
- Call arguments from the caller including defaults. - env :
Crinja
- The current environment. - target :
Crinja::Value
- The subject of the test. Short cut forarguments.target
.