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.

Easier way to render blocks without code duplication #1126

Closed
@Giwayume

Description

@Giwayume

If I have a template that uses lots of data from its struct, and I want to render individual blocks from the template (which also use the same data), there ends up being a lot of code duplication.

#[derive(Template)]
#[template(path = "my_template.html")]
pub struct MyTemplate<'a> {
    a: &'str,
    b: &'str,
    c: &'str,
    d: &'str,
    e: &'str,
    f: &'str,
    g: &'str,
}

#[derive(Template)]
#[template(path = "my_template.html", block = "sidebar")]
pub struct MySidebarTemplate<'a> {
    a: &'str,
    b: &'str,
    c: &'str,
    d: &'str,
    e: &'str,
    f: &'str,
    g: &'str,
}

MyTemplate { ... }.render()...
MySidebarTemplate { ... }.render()...

This is just a simple example, in a real application it's so much worse.

It would be nice if I could just take MyTemplate and tell it to render a specific block. Like:

MyTemplate { ... }.render_block("sidebar")

Conceptually, this could mean "render the full template, but discard everything from the HTML response that is not a part of the block", in terms of implementation. It may be a little more wasteful on CPU but it will save memory and clean up the code drastically.

Is something like this possible?

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