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 · 9 comments · May be fixed by #1112
Open

relative template paths and include_str support #877

henke443 opened this issue Oct 6, 2023 · 9 comments · May be fixed by #1112

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
Collaborator

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
Collaborator

djc commented Apr 11, 2024

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

@BuriKizilkaya
Copy link

I will try....

@andrewbaxter
Copy link

andrewbaxter commented Jul 29, 2024

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!

I think ATM the second option is also impossible to implement: rust-lang/rust#54725 (going on 6 years)

include_str and I assume doc are currently using compiler magic to do it IIUC.

i.e. I don't think there's a way to do this at the moment.

joshka referenced this issue in joshka/askama Dec 3, 2024
Templates can now be placed directly next to the source file that they
are defined in as a default. This relies on an unstable rust compiler
feature, which exposes the source file to proc macros. See
<rust-lang/rust#54725> for more info.

This requires the nightly compiler to run, and enabling the
proc_macro_span and procmacro2_semver_exempt cfg flags. To build / test:

```shell
RUSTFLAGS='--cfg proc_macro_span --cfg procmacro2_semver_exempt' \
  cargo +nightly build
```
Fixes: <https://github.com/djc/askama/issues/877>
@joshka joshka linked a pull request Dec 3, 2024 that will close this issue
3 tasks
joshka referenced this issue in joshka/askama Dec 3, 2024
Templates can now be placed directly next to the source file that they
are defined in as a default. This relies on an unstable rust compiler
feature, which exposes the source file to proc macros. See
<rust-lang/rust#54725> for more info.

This requires the nightly compiler to run, and enabling the
proc_macro_span and procmacro2_semver_exempt cfg flags. To build / test:

```shell
RUSTFLAGS='--cfg proc_macro_span --cfg procmacro2_semver_exempt' \
  cargo +nightly build
```
Fixes: <https://github.com/djc/askama/issues/877>
@joshka
Copy link

joshka commented Dec 3, 2024

i.e. I don't think there's a way to do this at the moment.

Use a nightly compiler, add cfg flags to your RUST_FLAGs and this PR does the trick:
https://github.com/djc/askama/pull/1112

Needs a bikeshed discussion on the feature flag name (as relative-paths is a little ambiguous for my liking), but it works.

From an implementation perspective, it made sense to me to just make the current source folder the first folder that gets searched for the template, rather than having to use any change in configuration. This allows some things to be put in the configured template folders and some things to be put in the relevant src folders. So the syntax to make this work is just #[template(path = "foo.html"].

One thing that is an open question is what to do when paths are specified in the config. I currently did not make this allow the relative paths, but it would be easy enough to extend the PR to do that. Requires a decision on this. (I haven't used config files generally so I don't really have a position on what is right there).

Additionally, it might be worth considering looking at files up the directory tree in order to have the ability to define templates that are generic / reused / default, and which can be overridden. This might be useful for situations where you have a standard list format and then have a template defined that will just pick that up from the parent folder, but sometimes want to override the list view. Likely outside of the scope of the PR, but worth considering

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

Successfully merging a pull request may close this issue.

8 participants