pub struct TemplateEngineLoader {
pub template_location: String,
pub extra_config_location: Option<String>,
}
tera-loader
only.Expand description
Helps loading Templates from disk along with a handy configuration file.
Templates should be placed in a flat file structure in one directory.
They may be accompanied by an extra.toml
file.
(by default, the location is configurable)
It also helps out a bit with deriving template realted configuration for the rest of whatever you are building.
Example:
use lib_humus::TemplateEngineLoader;
let template_loader = TemplateEngineLoader::new(
config.template.template_location.clone(),
config.template.extra_config.clone()
)
.cli_template_location(cli_args.template_location)
.cli_extra_config_location(cli_args.extra_config);
let templating_engine = match template_loader.load_templates() {
Ok(t) => t.into(),
Err(e) => {
println!("{e}");
::std::process::exit(1);
}
};
Fields§
§template_location: String
The path to the directory where the templates are.
extra_config_location: Option<String>
The path to the extra configuration (relative to the current pwd, not to the templates)
Implementations§
Source§impl TemplateEngineLoader
impl TemplateEngineLoader
Sourcepub fn new(
template_location: String,
extra_config_location: Option<String>,
) -> Self
pub fn new( template_location: String, extra_config_location: Option<String>, ) -> Self
Creates a new TemplateEngineLoader
with minimal typing.
Sourcepub fn cli_template_location(self, location: Option<String>) -> Self
pub fn cli_template_location(self, location: Option<String>) -> Self
Overrides the template location with a new location if it is set.
Intended for processing cli-options.
Sourcepub fn cli_extra_config_location(self, location: Option<String>) -> Self
pub fn cli_extra_config_location(self, location: Option<String>) -> Self
Overrides the extra configuration location with a new location if it is set.
Intended for processing cli-options.
Sourcepub fn base_dir(&self) -> String
pub fn base_dir(&self) -> String
Returns the template base directory.
Contructed by ensuring the template_location ends in a “/”.
Sourcepub fn load_templates(
&self,
) -> Result<HumusProtoEngine, TemplateEngineLoaderError>
pub fn load_templates( &self, ) -> Result<HumusProtoEngine, TemplateEngineLoaderError>
Initialize a HumusProtoEngine with the given templates and extra configuration.
Failure Modes:
- The
extra.toml
was not found and the path was explicitly set. - The
extra.toml
was found and is not valid toml. - The
extra.toml
passes, but tera finds an error in the templates.
If extra_config_location
is None
no error is returned if the extra.toml
was not found as the template might not require one.
Trait Implementations§
Source§impl Clone for TemplateEngineLoader
impl Clone for TemplateEngineLoader
Source§fn clone(&self) -> TemplateEngineLoader
fn clone(&self) -> TemplateEngineLoader
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more