class Crinja::Loader::BakedFileLoader(T)
- Crinja::Loader::BakedFileLoader(T)
- Crinja::Loader
- Reference
- Object
Overview
A loader that retrieves templates from a Baked File System.
This way templates can be baked directly into the executable, so there is no need to provide
separate templates files. However, this prevents modifying templates at runtime.
Both can be accomplished by using a ChoiceLoader
to combine a FileSystemLoader
with
baked files as a fallback/default template loader.
Usage depends on the shard schovi/baked_file_system
and this class must be explicitly required as crinja/loader/baked_file_loader
.
Usage:
require "crinja/loader/baked_file_loader"
module MyBakedTemplateFileSystem
BakedFileSystem.load("templates", __DIR__)
end
env.loader = Crinja::BakedFileLoader.new(MyBakedTemplateFileSystem)
# with choice loader:
env.loader = Crinja::Loader::ChoiceLoader.new([
Crinja::Loader::FileSystemLoader.new("/path/to/user/templates"),
Crinja::Loader::BakedFileLoader.new(MyBakedTemplateFileSystem),
])
See examples/kilt/kilt.cr
for a practical example in conjunction with Kilt
.
A baked file system can also be used as a default
Defined in:
loader/baked_file_loader.crConstructors
Instance Method Summary
- #file_system : T
-
#get_source(env : Crinja, template : String) : ::Tuple(String, String)
Get the template source, filename and reload helper for a template.
-
#list_templates
Iterates over all templates.
- #to_s(io)
Instance methods inherited from class Crinja::Loader
get_source(env : Crinja, template : String) : ::Tuple(String, String?)
get_source,
list_templates : Iterable(String)
list_templates,
load(env, name)
load
Constructor Detail
Instance Method Detail
Get the template source, filename and reload helper for a template.
It's passed the environment and template name and has to return a
tuple in the form {source : String, filename : String?}
or raise a
TemplateNotFoundError
if it can't locate the template.
The source part of the returned tuple must be the source of the
template as string. The filename should be the name of the file on
the filesystem if it was loaded from there, otherwise nil
.
The filename is used for the tracebacks if no loader extension is used.
Iterates over all templates. If the loader does not support that
it should raise a TypeError
which is the default behavior.