pub struct TemplatingEngine<ApiFormat>where
ApiFormat: HumusApiFormat,{
pub tera: HashMap<HumusFormatIdentifier, Tera>,
pub template_config: Option<ExtraConfig>,
pub templates_manifest: TemplatesManifest,
/* private fields */
}Expand description
Implements the actual data to text conversion using the tera crate.
Fields§
§tera: HashMap<HumusFormatIdentifier, Tera>An instance of the tera templating engine.
template_config: Option<ExtraConfig>If it was possible to read any extra configuration it will be stored here.
templates_manifest: TemplatesManifestThe template configuration read from the templates.toml manifest file.
Implementations§
Source§impl<ApiFormat> TemplatingEngine<ApiFormat>where
ApiFormat: HumusApiFormat,
impl<ApiFormat> TemplatingEngine<ApiFormat>where
ApiFormat: HumusApiFormat,
Sourcepub fn load_from_directory(
path: impl AsRef<Path>,
extra_config_path: Option<&Path>,
template_tera: Tera,
language_engine: Option<Arc<LanguageEngine>>,
) -> Result<Self, TemplatingEngineLoaderError>
pub fn load_from_directory( path: impl AsRef<Path>, extra_config_path: Option<&Path>, template_tera: Tera, language_engine: Option<Arc<LanguageEngine>>, ) -> Result<Self, TemplatingEngineLoaderError>
Load this templating engine from a path to a templates directory and an optional path to a template configuration file (extra.toml).
teraexpects an empty Tera engine, that you have already added your customizations to. UseTera::default()if you do not need to customize.- When a
language_engineis supplied this automatically sets up theTextFunction(text()inside the template) with an implicit path to pass the language through.
The following is expeced inside the path:
{path}/tera2/- A directory containing the tera templates (This is different from previous versions of lib-humus where the templates were in the path directly){view_id}.{format_extension}Each template must be named after the id of the view it represents taken from HumusView::get_template_name and the extension that is specified in the templates manifest.- Additional templates can be specified using the
additional_templateskey in each format description.
templates.toml- Must contain the templates manifestextra.toml- Optional file that contains default template configuration is loaded from whenextra_config_pathisNone.
Sourcepub fn render_view<S: HumusQuerySettings>(
&self,
settings: &S,
view: impl HumusView<S, ApiFormat>,
language_engine: &LanguageEngine,
) -> Response
pub fn render_view<S: HumusQuerySettings>( &self, settings: &S, view: impl HumusView<S, ApiFormat>, language_engine: &LanguageEngine, ) -> Response
Takes settings and a view, converting it to a serveable response.
Example:
async fn hello_world_handler(
State(arc_state): State<Arc<ServiceSharedState>>,
Extension(settings): Extension<QuerySettings>,
) -> Response {
let state = Arc::clone(&arc_state);
state.templating_engine.render_view(
&settings,
View::Message{
title: "Hey There!".to_string(),
message: "You are an awesome creature!".to_string()
},
)
}
This function can, depending on the provided settings and the ApiFormat type operate in either template mode or API mode.
It operates in API mode when the from_name() method of the ApiFormat type returns a Some, otherwise it operates in template mode.
If the requested format id neither resolves to a valid API nor an existing template a status code 400 and a response body starting with (unknown format) followed by a human readable error message is generated as the response.
In template mode the following happens:
- status code is fetched from the
view. - Template name and MimeType are fetched.
- The Template context is populated with metadata from the
viewand template_config. (Documentation linked below) - The initalize_template_context() hook method
is called on
view. - The template gets rendered, resulting in further processing or an error response.
- The response is constructed using the MimeType from earlier and the text from the template.
- The update_response() hook method is called on
view - If the status code of the response from
get_api_response()is 200 it will be replaced with the result of theget_status_code()method of theview.
See also the writing templates documentation.
In API mode the following happens:
- status code is fetched from the
view. - The
into_api_response()method of theviewis used to generate an HTTP response. - If the status code of the response from
get_api_response()is 200 it will be replaced with the result of theget_status_code()method of theview.
Trait Implementations§
Source§impl<ApiFormat> Clone for TemplatingEngine<ApiFormat>where
ApiFormat: HumusApiFormat + Clone,
impl<ApiFormat> Clone for TemplatingEngine<ApiFormat>where
ApiFormat: HumusApiFormat + Clone,
Source§fn clone(&self) -> TemplatingEngine<ApiFormat>
fn clone(&self) -> TemplatingEngine<ApiFormat>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more