This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 219
Load templates from relative paths #1112
Open
joshka
wants to merge
1
commit into
askama-rs:main
Choose a base branch
from
joshka:jm/relative-paths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #[cfg(feature = "relative-paths")] | ||
| mod relative_paths { | ||
| use askama::Template; | ||
|
|
||
| #[derive(Template)] | ||
| #[template(path = "relative_paths.txt")] | ||
| struct RelativePathTemplate { | ||
| name: String, | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_relative_paths() { | ||
| let t = RelativePathTemplate { | ||
| name: "world".to_string(), | ||
| }; | ||
| assert_eq!(t.render().unwrap(), "Hello, world!"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Hello, {{ name }}! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, ... |
||
| #[cfg(feature = "relative-paths")] | ||
| { | ||
| let source = proc_macro2::Span::call_site().source_file(); | ||
| default_dirs = if source.is_real() { | ||
| let relative_path = source.path().parent().unwrap().to_path_buf(); | ||
| vec![relative_path, root_path] | ||
| } else { | ||
| vec![root_path] | ||
| }; | ||
| } | ||
| #[cfg(not(feature = "relative-paths"))] | ||
| { | ||
| default_dirs = vec![root_path]; | ||
| } | ||
|
|
||
| let mut syntaxes = BTreeMap::new(); | ||
| syntaxes.insert(DEFAULT_SYNTAX_NAME.to_string(), Syntax::default()); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding the
document-featurescrate so this info can automatically populate the lib.rs doc comment without having to duplicate it.