askama
askama copied to clipboard
relative template paths and include_str support
I would want to have the feature.html template be in the same folder as feature.rs.
It would be cool if you could do either one of:
A)
pub static FEATURE_TEMPLATE_HTML: &'static str = include_str!("feature.html");
#[derive(Template)]
#[template(source=FEATURE_TEMPLATE_HTML, ext = "html")]
struct featureTemplate<'a> {
name: &'a str,
}
B)
#[derive(Template)]
#[template(path="./feature.html")] // And maybe just path="feature.html" is using the toml dirs? Like how "import" works in nodejs
struct featureTemplate<'a> {
name: &'a str,
}
I think I'm open to either of these (assuming the implemention doesn't add too much complexity), if you want take a whack at implementing this. Have a look at https://github.com/djc/askama/blob/main/askama_derive/src/generator.rs#L81 and https://github.com/djc/askama/blob/main/askama_derive/src/input.rs#L11.
The first option is impossible to implement. The proc macro will only see the token FEATURE_TEMPLATE_HTML
, and it is currently not possible to evaluate the context. (E.g. that's why #[doc = include_str()]
works, but #[doc = SOME_CONSTANT]
does not.)
But I like the second option!
Yes please! I REALLY wish to co-locate the html file with the rust file. I've actively looked for alternatives to askama that does this, but I haven't found any, and I don't want to use anything else because I really like how askama does things. I doubt I'm able to implement this myself, I'll take a look but I otherwise that someone else is able to!
I'm currently using this:
Create an empty templates
directory, then use a relative path:
#[derive(Template)]
#[template(path = "../src/some/thing.yml")]
struct Some {
foo: String
}
Downside is that you have to hard code the entire path
Is there any progress?
No, I guess there isn't. Do you want to submit a PR?
I will try....