This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Description
I have a template that I call a View. It has implementations which return templates-in-templates with derived values. Sometimes these can fail and return results. These results need to be handled inside the template, even though both arms of Result<> implement Display.
/// View for editing a single post.
#[derive(Template)]
#[template(path = "post/edit.html")]
pub struct PostEditView {
pub context: Context,
pub attachment_collection: AttachmentCollection,
pub post_collection: PostCollection,
pub post_form: PostForm,
}
impl PostEditView {
pub fn post_edit_template(&self) -> PostFormPartial {
PostFormPartial {
attachment_collates: Default::default(),
post_collate: None,
post_form: &self.post_form,
thread_collate: None,
}
}
pub fn post_template(&self) -> Result<impl Template + '_, Error> {
Ok(PostPartial {
attachment_collates: self.attachment_collection.collates(),
post_collate: self.post_collection.collate_single()?,
post_position: None,
})
}
}
My template could look like this:
{% block content %}
{{ self.post_template() }}
{{ self.post_edit_template() }}
{% endblock %}
Instead, it must look like this:
{% block content %}
{% self.post_template() }}
{% when Ok(post) %}
{{ post }}
{% when Err(e) %}
{{ e }}
{% endmatch %}
{{ self.post_edit_template() }}
{% endblock %}
I don't like this. It would be super cool if there was a way to simply {{ result }} and render either branch automatically.