pub struct HumusEngine<V, S, F>{
pub tera: Tera,
pub template_config: Option<Table>,
/* private fields */
}
axum-view
only.Expand description
๐ Tera based Templating Engine that writes out axum Response structs.
The engine uses logic and data from the configured View (V) type as well as data and configuration provided by the QuerySettings (S) type to produce the desired response with minimal code overhead inside your main logic.
Note: The private fields are PhantomData because the V, S and N generics are only used for the implementation.
The Engine operates in one of two modes determined by the Format (F) which is provided by the QuerySettings:
- Template Mode: The Format describes which template to use for rendering the View to some kind of text using Tera templates.
- API Mode: The View serializes itself, usually to a Json Response.
ยงTemplate Mode
In template mode the following sequence of events happens:
- status code, format and cookies are fetched.
(The cookie functionality is enabled with the
axum-view+cookie
feature) - Template name and MimeType are fetched.
- The Template context is populated with metadatam the View and
template_config
. - The QuerySettings are given the chance to populate the context
with their own values using the
initalize_template_context()
hook. - 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 View is given a chance to alter the Response
using the
update_response()
hook. - If the status code of the Response is โ200 OKโ (the default) the status code and cookies fetched earlier are applied.
ยงTemplate Symbols
The symbols defined for inside templates are:
view
will contain the template name, that coincides with the basename of the template file.format
will contain the format name (i.e.html
,text
,json
)mime_type
will contain the serialized mime_type (i.e.text/plain; charset=utf-8
orapplication/json
)http_status
will contain the numeric HTTP status code.data
will contain the serde serialized Viewextra
will be set totemplate_config
which usually comes from anextra.toml
file in the template directory or a configured custom location.- others that were added by the QuerySettings in
initalize_template_context()
ยงAPI Mode
In API mode the following, simpler sequence of events happens:
- status code, format and cookies are fetched.
(The cookie functionality is enabled with the
axum-view+cookie
feature) - The View serializes itself using the
get_api_response()
callback. - If the status code of the Response is โ200 OKโ (the default) the status code and cookies fetched earlier are applied.
Fieldsยง
ยงtera: Tera
An instance of the tera templating engine.
template_config: Option<Table>
If it was possible to read any extra configuration it will be stored here.
Implementationsยง
Sourceยงimpl<V, S, F> HumusEngine<V, S, F>
impl<V, S, F> HumusEngine<V, S, F>
Sourcepub fn new(tera: Tera, template_config: Option<Table>) -> Self
pub fn new(tera: Tera, template_config: Option<Table>) -> Self
Creates a new Templating Engine.
An alternative would be converting from a HumusProtoEngine.
Sourcepub fn render_view(&self, settings: &S, view: V) -> Response
pub fn render_view(&self, settings: &S, view: V) -> 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()
},
)
}
Trait Implementationsยง
Sourceยงimpl<V, S, F> Clone for HumusEngine<V, S, F>
impl<V, S, F> Clone for HumusEngine<V, S, F>
Sourceยงfn clone(&self) -> HumusEngine<V, S, F>
fn clone(&self) -> HumusEngine<V, S, F>
1.0.0 ยท Sourceยงfn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more