askama icon indicating copy to clipboard operation
askama copied to clipboard

relative template paths and include_str support

Open henke443 opened this issue 8 months ago • 7 comments

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, 
}

henke443 avatar Oct 06 '23 09:10 henke443

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.

djc avatar Oct 06 '23 09:10 djc

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!

Kijewski avatar Oct 06 '23 09:10 Kijewski

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!

Zonnex avatar Oct 12 '23 17:10 Zonnex

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

qwfy avatar Dec 18 '23 16:12 qwfy

Is there any progress?

BuriKizilkaya avatar Apr 10 '24 21:04 BuriKizilkaya

No, I guess there isn't. Do you want to submit a PR?

djc avatar Apr 11 '24 14:04 djc

I will try....

BuriKizilkaya avatar Apr 11 '24 14:04 BuriKizilkaya