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

relative template paths and include_str support #877

Open
henke443 opened this issue Oct 6, 2023 · 7 comments
Open

relative template paths and include_str support #877

henke443 opened this issue Oct 6, 2023 · 7 comments

Comments

@henke443
Copy link

henke443 commented Oct 6, 2023

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, 
}
@djc
Copy link
Owner

djc commented Oct 6, 2023

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.

@Kijewski
Copy link
Collaborator

Kijewski commented Oct 6, 2023

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!

@Zonnex
Copy link

Zonnex commented Oct 12, 2023

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!

@qwfy
Copy link

qwfy commented Dec 18, 2023

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

@BuriKizilkaya
Copy link

Is there any progress?

@djc
Copy link
Owner

djc commented Apr 11, 2024

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

@BuriKizilkaya
Copy link

I will try....

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

6 participants