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,
Auto Trait Implementations§
impl<ApiFormat> !RefUnwindSafe for TemplatingEngine<ApiFormat>
impl<ApiFormat> !UnwindSafe for TemplatingEngine<ApiFormat>
impl<ApiFormat> Freeze for TemplatingEngine<ApiFormat>where
PhantomData<ApiFormat>: Freeze,
impl<ApiFormat> Send for TemplatingEngine<ApiFormat>where
PhantomData<ApiFormat>: Send,
impl<ApiFormat> Sync for TemplatingEngine<ApiFormat>where
PhantomData<ApiFormat>: Sync,
impl<ApiFormat> Unpin for TemplatingEngine<ApiFormat>where
PhantomData<ApiFormat>: Unpin,
impl<ApiFormat> UnsafeUnpin for TemplatingEngine<ApiFormat>where
PhantomData<ApiFormat>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<F, T> ConvertInto<T> for Fwhere
T: ConvertFrom<F>,
impl<F, T> ConvertInto<T> for Fwhere
T: ConvertFrom<F>,
Source§fn convert_into(self) -> T
fn convert_into(self) -> T
Self to a value of type T.Source§impl<F, T> ConvertTryFrom<F> for Twhere
F: ConvertInto<T>,
impl<F, T> ConvertTryFrom<F> for Twhere
F: ConvertInto<T>,
Source§impl<F, T> ConvertTryInto<T> for Fwhere
T: ConvertTryFrom<F>,
impl<F, T> ConvertTryInto<T> for Fwhere
T: ConvertTryFrom<F>,
Source§type Error = <T as ConvertTryFrom<F>>::Error
type Error = <T as ConvertTryFrom<F>>::Error
Source§fn convert_try_into(self) -> Result<T, <T as ConvertTryFrom<F>>::Error>
fn convert_try_into(self) -> Result<T, <T as ConvertTryFrom<F>>::Error>
Self to a value of type T.impl<T> ErasedDestructor for Twhere
T: 'static,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more