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

Forced template root dir #81

Closed
trsh opened this Issue Apr 25, 2018 · 15 comments

Comments

Projects
None yet
4 participants
@trsh
Copy link

trsh commented Apr 25, 2018

The path is interpreted as relative to the templates dir in the directory where the originating crate's Cargo.toml resides.

Why? This forces me a 1 way structure, that works bad with my project tree. Please add an option to change the root dir, or better to add multiple ones.

@djc

This comment has been minimized.

Copy link
Owner

djc commented Apr 25, 2018

I built it this way because it's simple and fairly easy to use.

Can you explain more about how your project tree is laid out, and how you would like Askama to work in your context?

@trsh

This comment has been minimized.

Copy link
Author

trsh commented Apr 25, 2018

I use actix FW and have multiple apps attached to server. Each app with it's underlying logic lives in separate Directory, where I also would like to add the templates.

Nerveless I can't really come up with an Idea how to organise this. One thing that comes to my mind, is that I have a choice to load the templates relative to dir where #[template(path = ".../templates/layout.html")] lives.

@trsh

This comment has been minimized.

Copy link
Author

trsh commented Apr 25, 2018

However this is not a blocker for me, #82 is :/

@mashedcode

This comment has been minimized.

Copy link
Contributor

mashedcode commented Jun 28, 2018

@djc Thank you for your work. I really like askama.

Can you explain more about how your project tree is laid out, and how you would like Askama to work in your context?

There're a lot of reasons why someone might want to put templates in a different directory.

Example:
One might want have a bunch of encapsulated modules that each consist of one template.
A project tree like that might be laid out like this:

modules
| Cargo.toml
| lib.rs
| foo
| | layout.html (askama template)
| | mod.rs (Makes use of this crate to load askama template)
| | * Some more module related files.
| bar
| | layout.html
| | mod.rs

The current work around requires one to have an empty templates/ directory and specify a relative template path as already mentioned by @trsh which is not ideal.

One backwards compatible approach to solve this would be to configure the default behavior by switches in Cargo.toml. As a bonus these switches may also be available as paramaters to when using #[template(.

For example:

[askama]
# Whether the path should be relative to the rust source file. (default: false)
relative = true
# Path to template directory which is relative to cargo manifest dir. (default: "templates") 
path = ""

One may also want to treat paths in #[template(path which start with a leading slash as actual absolute paths.

@mashedcode

This comment has been minimized.

Copy link
Contributor

mashedcode commented Jun 28, 2018

@djc I'll be happy to implement whatever approach you think is best. The above is only my idea. You might have a better one.

@djc

This comment has been minimized.

Copy link
Owner

djc commented Jun 29, 2018

Here's the design I had in mind: there will be an optional askama.toml in the crate root (so, next Cargo.toml). It will contain any Askama configuration, all of which will be optional. The first option is like this:

[general]
dirs = ["templates", "foo/templates", "../bar/baz/templates"]

These are all paths relative to the crate root, and they are searched in order by get_template_source().

Does that sound good?

@mashedcode

This comment has been minimized.

Copy link
Contributor

mashedcode commented Jun 29, 2018

Sounds good.

@djc

This comment has been minimized.

Copy link
Owner

djc commented Jul 4, 2018

Since I had a question about this on Gitter: I looked at what it could take to do template paths relative to the file defining the context struct. Unfortunately procedural macros right now don't give me a reliable way to figure out the path to the source code, as far as I can tell. Once they do, I definitely wouldn't mind revisiting that as an option, but the paths would have to be marked in some way (either with a path prefix like ./ or with a different attribute name like rel).

@djc

This comment has been minimized.

Copy link
Owner

djc commented Jul 4, 2018

@mashedcode so are you still interested in working on the design as proposed?

@mashedcode

This comment has been minimized.

Copy link
Contributor

mashedcode commented Jul 4, 2018

I'm still interested.

@djc

This comment has been minimized.

Copy link
Owner

djc commented Jul 4, 2018

Nice, I'm happy to provide further guidance/mentoring if needed; let me know.

@trsh

This comment has been minimized.

Copy link
Author

trsh commented Jul 5, 2018

Me too.

@denysvitali

This comment has been minimized.

Copy link

denysvitali commented Jul 6, 2018

I also noticed that it isn't possible to add subdirs to the templates folder. For instance, the following attribute will result in a compilation fail:

#[derive(Template)]
#[template(path = "correspondents/add.html")]

with the following error:

  Compiling dmsrs v0.1.0 (file:///home/dvitali/Documents/rust/dmsrs)
error: proc-macro derive panicked
  --> src/routes/correspondents.rs:24:10
   |
24 | #[derive(Template)]
   |          ^^^^^^^^
   |
   = help: message: no entry found for key

error: aborting due to previous error

error: Could not compile `dmsrs`.
@djc

This comment has been minimized.

Copy link
Owner

djc commented Jul 6, 2018

@denysvitali that should be a separate issue. I just tried to create a test to reproduce your error, but that didn't work. Can you give me more detailed steps to reproduce, ideally in a separate issue? Bonus points if you submit it as a PR to the test suite. 😄

diff --git a/testing/templates/sub/inner.html b/testing/templates/sub/inner.html
new file mode 100644
index 0000000..db8a8a4
--- /dev/null
+++ b/testing/templates/sub/inner.html
@@ -0,0 +1 @@
+{{ foo }}
diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs
index 95c14b1..d9b3fcd 100644
--- a/testing/tests/simple.rs
+++ b/testing/tests/simple.rs
@@ -258,3 +258,14 @@ struct Empty;
 fn test_empty() {
     assert_eq!(Empty.render().unwrap(), "foo");
 }
+
+#[derive(Template)]
+#[template(path = "sub/inner.html")]
+struct Subdir {
+    foo: String,
+}
+
+#[test]
+fn test_subdir() {
+    assert_eq!(Subdir { foo: "bar".into() }.render().unwrap(), "bar");
+}
@denysvitali

This comment has been minimized.

Copy link

denysvitali commented Jul 6, 2018

@djc Sure thing! I'll try create a minimal reproducible test, make a new issue and a PR to fix the problem / add a test (or both 😄)

Edit: Nevermind, apparently the problem was with my template file: I was extending a template file, by using {% extends "../menupage.html" %}, but apparently the extends path isn't relative to the file (it is relative to the template directory)

@djc djc closed this in #103 Jul 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.