djc / askama

Type-safe, compiled Jinja-like templates for Rust
Apache License 2.0
3.46k stars 218 forks source link

Multiple configs? #703

Open saward opened 2 years ago

saward commented 2 years ago

Hi,

I'm experimenting with a setup where I have multiple binaries, and some shared code between all those binaries, including shared routes for HTML, rendered via askama. For example, I may have for the login page:

#[derive(Template)]
#[template(path = "html/login.html")]
pub struct LoginPayload {
    payload: TemplatePayload,
    providers: Vec<ProviderLink>,
}

This is in a shared library across all the binaries. However, each binary I would like to have its own set of templates. E.g., its own implementation of html/login.html.

Now, if I set askama.toml to point to the templates for one binary, then the other binaries will get the wrong templates folder.

Similarly, if I use the config value like so:

#[derive(Template)]
#[template(path = "html/login.html", config = "mysite/askama.toml")]
pub struct LoginPayload {
    payload: TemplatePayload,
    providers: Vec<ProviderLink>,
}

Then I still can't have a per-site implementation of html/login.html. What I really need is a way to specify the config file location via an environment variable or similar, so it can be specific to each site. Is there a way to do this, or another way to solve this problem currently?

Short of submitting a PR to allow this, I might just need to have a specific command to run each site that generates the askama.toml file each time. This might cause some issues if I try to run multiple different binaries at the same time, though that may not actually be an issue.

Thanks!

djc commented 2 years ago

Askama really doesn't support this use case, and it's not clear if it makes sense to do so. One thing that we've briefly discussed would be to allow "offline" code generation (controlled by the caller). If we would implement that, you could supposedly generate multiple copies of the templates with a separate Config. Would you be willing to contribute code towards this idea?