File tree 6 files changed +48
-1
lines changed 6 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,12 @@ with-axum = ["askama_derive/with-axum"]
29
29
with-rocket = [" askama_derive/with-rocket" ]
30
30
with-warp = [" askama_derive/with-warp" ]
31
31
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
+
32
38
[dependencies ]
33
39
askama_derive = { version = " 0.13" , path = " ../askama_derive" }
34
40
askama_escape = { version = " 0.11" , path = " ../askama_escape" }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ Hello, {{ name }}!
Original file line number Diff line number Diff line change @@ -24,6 +24,12 @@ with-axum = []
24
24
with-rocket = []
25
25
with-warp = []
26
26
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
+
27
33
[dependencies ]
28
34
parser = { package = " askama_parser" , version = " 0.3.1" , path = " ../askama_parser" }
29
35
mime = " 0.3"
Original file line number Diff line number Diff line change @@ -26,7 +26,22 @@ impl<'a> Config<'a> {
26
26
template_whitespace : Option < & str > ,
27
27
) -> std:: result:: Result < Config < ' a > , CompileError > {
28
28
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
+ }
30
45
31
46
let mut syntaxes = BTreeMap :: new ( ) ;
32
47
syntaxes. insert ( DEFAULT_SYNTAX_NAME . to_string ( ) , Syntax :: default ( ) ) ;
Original file line number Diff line number Diff line change 1
1
#![ deny( elided_lifetimes_in_paths) ]
2
2
#![ deny( unreachable_pub) ]
3
+ #![ cfg_attr( feature = "relative-paths" , feature( proc_macro_span) ) ]
3
4
4
5
use std:: fmt;
5
6
use std:: { borrow:: Cow , collections:: HashMap } ;
You can’t perform that action at this time.
0 commit comments