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

Nested templates won't work #82

Closed
trsh opened this issue Apr 25, 2018 · 4 comments
Closed

Nested templates won't work #82

trsh opened this issue Apr 25, 2018 · 4 comments

Comments

@trsh
Copy link

trsh commented Apr 25, 2018

let tmpl = DefaultTemplate { name: "world" };
  let tmpl_body = tmpl.render().unwrap();
  
  let layout = DefaultLayout { content: &tmpl_body };
  let layout_body = layout.render().unwrap();
  
  Ok(HttpResponse::Ok().content_type("text/html").body(layout_body))

tmpl_body is rendered as an String to HTML body. Is there a away around this?

@djc
Copy link
Collaborator

djc commented Apr 25, 2018

Can you provide a minimal example (including the templates), and explain your expected outcome vs the actual outcome?

@trsh
Copy link
Author

trsh commented Apr 25, 2018

clone https://github.com/trsh/examples
cd into template_askama
cargo build & cargo run

I expect the tmpl_body to appear in Layout as an HTML, not plain text.

The mine idea behind this, is that I don't want to Repeat stuff, that is General for all templates, like <html><body>(general css includes, favicon, etc, etc.). That's a lot of copy+paste and can even lead to huge problems in larger scale project. That's why introducing layouts logic.

@trsh
Copy link
Author

trsh commented Apr 25, 2018

Somehave I missed https://docs.rs/askama/0.6.3/askama/#template-inheritance :(( sorry

@trsh trsh closed this as completed Apr 25, 2018
@djc
Copy link
Collaborator

djc commented Apr 25, 2018

Here's how you can fix this:

--- a/template_askama/templates/layout.html
+++ b/template_askama/templates/layout.html
@@ -5,6 +5,6 @@
   <title>Actix web</title>
 </head>
 <body>
-  {{content}}
+  {{content|safe}}
 </body>
 </html>
\ No newline at end of file

That is, the including template does not know whether the included template is safe to display without escaping. By simply marking it as safe with the safe filter, it will do what you expect.

And yes, I was going to say next that template inheritance is probably a better solution for what you're trying to do here.

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

No branches or pull requests

2 participants