Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier way to render blocks without code duplication #1126

Open
Giwayume opened this issue Feb 6, 2025 · 1 comment
Open

Easier way to render blocks without code duplication #1126

Giwayume opened this issue Feb 6, 2025 · 1 comment

Comments

@Giwayume
Copy link

Giwayume commented Feb 6, 2025

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?

@Kijewski
Copy link
Collaborator

Kijewski commented Feb 6, 2025

A similar idea was asked in rinja-rs/rinja#328. Maybe you can answer if you think my idea is worth exploring?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants