Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit b6106c5

Browse files
committed
Load templates from relative paths
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: <#877>
1 parent 704f8f1 commit b6106c5

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

askama/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ with-axum = ["askama_derive/with-axum"]
2929
with-rocket = ["askama_derive/with-rocket"]
3030
with-warp = ["askama_derive/with-warp"]
3131

32+
## Enables the ability to put templates in a directory relative to the source file that uses them.
33+
## Requires a nightly compiler and adding:
34+
## RUSTFLAGS='--cfg proc_macro_span --cfg procmacro2_semver_exempt'
35+
## to your cargo build command.
36+
relative-paths = ["askama_derive/relative-paths"]
37+
3238
[dependencies]
3339
askama_derive = { version = "0.13", path = "../askama_derive" }
3440
askama_escape = { version = "0.11", path = "../askama_escape" }

askama/tests/relative_paths.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#[cfg(feature = "relative-paths")]
2+
mod relative_paths {
3+
use askama::Template;
4+
5+
#[derive(Template)]
6+
#[template(path = "relative_paths.txt")]
7+
struct RelativePathTemplate {
8+
name: String,
9+
}
10+
11+
#[test]
12+
fn test_relative_paths() {
13+
let t = RelativePathTemplate {
14+
name: "world".to_string(),
15+
};
16+
assert_eq!(t.render().unwrap(), "Hello, world!");
17+
}
18+
}

askama/tests/relative_paths.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, {{ name }}!

askama_derive/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ with-axum = []
2424
with-rocket = []
2525
with-warp = []
2626

27+
## Enables the ability to put templates in a directory relative to the source file that uses them.
28+
## Requires a nightly compiler and adding:
29+
## RUSTFLAGS='--cfg proc_macro_span --cfg procmacro2_semver_exempt'
30+
## to your cargo build command.
31+
relative-paths = []
32+
2733
[dependencies]
2834
parser = { package = "askama_parser", version = "0.3.1", path = "../askama_parser" }
2935
mime = "0.3"

askama_derive/src/config.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,22 @@ impl<'a> Config<'a> {
2626
template_whitespace: Option<&str>,
2727
) -> std::result::Result<Config<'a>, CompileError> {
2828
let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
29-
let default_dirs = vec![root.join("templates")];
29+
let root_path = root.join("templates");
30+
let default_dirs;
31+
#[cfg(feature = "relative-paths")]
32+
{
33+
let source = proc_macro2::Span::call_site().source_file();
34+
default_dirs = if source.is_real() {
35+
let relative_path = source.path().parent().unwrap().to_path_buf();
36+
vec![relative_path, root_path]
37+
} else {
38+
vec![root_path]
39+
};
40+
}
41+
#[cfg(not(feature = "relative-paths"))]
42+
{
43+
default_dirs = vec![root_path];
44+
}
3045

3146
let mut syntaxes = BTreeMap::new();
3247
syntaxes.insert(DEFAULT_SYNTAX_NAME.to_string(), Syntax::default());

0 commit comments

Comments
 (0)