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

Template Rendering of a Result<T + Display, E + Display> Cleanly #1059

@jaw-sh

Description

@jaw-sh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions