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

Add possibility to specify template directories #103

Merged
merged 2 commits into from
Jul 10, 2018
Merged

Add possibility to specify template directories #103

merged 2 commits into from
Jul 10, 2018

Conversation

mashedcode
Copy link
Contributor

Resolves #81

I didn't do extensive testing on this because I don't actually use askama in production yet. But it should work. I think.

@djc
Copy link
Collaborator

djc commented Jul 4, 2018

This looks pretty good! I have a bunch of minor concerns, will do a detailed review tomorrow.

Copy link
Collaborator

@djc djc left a comment

Choose a reason for hiding this comment

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

Here's some more detailed feedback. Let me know what you think!

@@ -393,7 +393,10 @@ fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> {
/// that have templates, to make sure the crate gets rebuilt when template
/// source code changes.
pub fn rerun_if_templates_changed() {
visit_dirs(&path::template_dir(), &|e: &DirEntry| {
let trigger_rerun = &|e: &DirEntry| {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why pre-define this here? Did you try just passing the closure inside the loop?

@@ -10,10 +10,12 @@ workspace = ".."

[features]
default = []
serde-json = ["serde", "serde_json"]
serde-json = []
Copy link
Collaborator

Choose a reason for hiding this comment

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

So serde is now required for deserializing the configuration, but I think serde_json should still be optional, as before, no?


pub fn template_dirs() -> Vec<PathBuf> {
let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let askama = root.join("askama.toml");
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would like to move "askama.toml" into a constant, maybe named CONFIG_FILE_NAME. Then, please move the code to instantiate a Config into a Config::from_file() method (which doesn't need any arguments).

Also, here and in other places, please don't override the configuration if it provides an empty list. In that case, it should probably just do nothing (this would allow users to enforce the use of source-based templates only, I guess).

default
}
} else {
vec![root.join("templates")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems to be a duplicate of the default defined earlier.

path.push(tpl_path);
let dirs = template_dirs();
let path = search_template_in_dirs(&dirs, tpl_path)
.expect(&format!("Template file '{}' does not exist.", tpl_path.to_str().unwrap()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: elsewhere I use sentence fragments for errors, not full sentences. Please don't capitalize, and don't end with a period.

@@ -5,9 +5,17 @@ use std::path::{Path, PathBuf};

use toml;

fn search_template_in_dirs(dirs: &Vec<PathBuf>, path: &Path) -> Option<PathBuf> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: this can take a slice instead of a Vec, so &[PathBuf].

fs_rel_path.push(rel);
fs_rel_path = fs_rel_path.with_file_name(path);
let root = search_template_in_dirs(&dirs, rel).unwrap();
let fs_rel_path = root.join(rel).with_file_name(path);
Copy link
Collaborator

Choose a reason for hiding this comment

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

One question is whether the start_at paths here are already rooted (that is, are already absolute paths starting with the crate root) or not. Probably need to audit all uses to see whether usage is consistent or not.

Copy link
Contributor Author

@mashedcode mashedcode Jul 5, 2018

Choose a reason for hiding this comment

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

Just preserving existing behavior. If start_at is already rooted the relative path should not exist and it should slip through. This was probably meant for includes where one may not specify the sub-directory the template is in.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay, but can we at least replace the unwrap() with an expect() that clearly states the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

let mut fs_rel_path = root.clone();
fs_rel_path.push(rel);
fs_rel_path = fs_rel_path.with_file_name(path);
let root = search_template_in_dirs(&dirs, rel).unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure unwrap() makes sense here?

Copy link
Contributor Author

@mashedcode mashedcode Jul 5, 2018

Choose a reason for hiding this comment

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

I think since start_at is defined this is a path that already existed previously. It just forgot in which directory it existed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This should also expect() instead of unwrap().

@djc
Copy link
Collaborator

djc commented Jul 10, 2018

@mashedcode anything else you want to change? Otherwise I'll just merge this and iterate on the design a little bit. Thanks for working on this!

@mashedcode
Copy link
Contributor Author

Sorry, wouldn't know how to design it better at the moment. Therefore there's nothing I plan to change at the moment.

@djc djc merged commit b5b1589 into rinja-rs:master Jul 10, 2018
@djc
Copy link
Collaborator

djc commented Jul 10, 2018

If you're interested, here's a bunch of the follow-up I had in mind: https://github.com/djc/askama/compare/b5b1589...f74d121. There's probably a bit more, I wanted to store a Config in Input and move find_template_from_path() into Config.

@mashedcode mashedcode deleted the template-dir branch July 19, 2018 10:08
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.

2 participants