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

Load templates from relative paths #1112

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joshka
Copy link

@joshka joshka commented 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:

RUSTFLAGS='--cfg proc_macro_span --cfg procmacro2_semver_exempt' \
  cargo +nightly build

Fixes: https://github.com/djc/askama/issues/877


TODO:

  • docs
  • work out the right name for the feature flag (there's likely a less ambiguous name than relative-paths for this)
  • work out if it's possible to compile this without using the nightly compiler. The proc-macro2 crate has shims replacements for the unstable nightly code which can be enabled somehow, but I haven't dug deep enough to understand how / why that is.

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 force-pushed the jm/relative-paths branch from 381cb2c to b6106c5 Compare December 3, 2024 04:52
Comment on lines +32 to +35
## Enables the ability to put templates in a directory relative to the source file that uses them.
## Requires a nightly compiler and adding:
## RUSTFLAGS='--cfg proc_macro_span --cfg procmacro2_semver_exempt'
## to your cargo build command.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding the document-features crate so this info can automatically populate the lib.rs doc comment without having to duplicate it.

@@ -26,7 +26,22 @@ impl<'a> Config<'a> {
template_whitespace: Option<&str>,
) -> std::result::Result<Config<'a>, CompileError> {
let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let default_dirs = vec![root.join("templates")];
let root_path = root.join("templates");
let default_dirs;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probable that the logic of which dirs end up in the list when config is taken into account might have to be a little more complex than this current logic. The crux of the problem is solved however, leaving the choice of how it should be behave remaining. I.e. if the user has a config file and it has dirs specified should we still resolve relative paths, should there be a config for this, ...

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 this pull request may close these issues.

relative template paths and include_str support
1 participant