From 78296185a520287976edde21343513e2ef18594c Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Tue, 25 Jun 2024 19:09:21 +0200 Subject: [PATCH 1/2] Add failing test. --- examples/simple/src/main.rs | 15 +++++----- .../templates/page/intermediate.rs.html | 12 ++++++++ examples/simple/templates/page/page.rs.html | 4 +-- examples/warp03/templates/base.rs.html | 21 ++++++++++++++ examples/warp03/templates/error.rs.html | 29 +++++-------------- .../warp03/templates/intermediate.rs.html | 7 +++++ examples/warp03/templates/page.rs.html | 20 ++----------- 7 files changed, 60 insertions(+), 48 deletions(-) create mode 100644 examples/simple/templates/page/intermediate.rs.html create mode 100644 examples/warp03/templates/base.rs.html create mode 100644 examples/warp03/templates/intermediate.rs.html diff --git a/examples/simple/src/main.rs b/examples/simple/src/main.rs index 835326a..659d76d 100644 --- a/examples/simple/src/main.rs +++ b/examples/simple/src/main.rs @@ -257,16 +257,17 @@ fn test_page_with_base() { r2s(|o| page::page_html(o, "World")), "\ \n\ - \n Hello World!\ - \n \ + \n Hello World!\n\ + \n \n\ \n\ \n \ \n

Hello World!

\ - \n \ - \n

This is page content for World

\ - \n\ - \n \ - \n\n\n" + \n \n
\ + \n \ + \n

This is page content for World

\n\ + \n
\n\ + \n\n \ + \n\n\n\n" ); } diff --git a/examples/simple/templates/page/intermediate.rs.html b/examples/simple/templates/page/intermediate.rs.html new file mode 100644 index 0000000..3e29127 --- /dev/null +++ b/examples/simple/templates/page/intermediate.rs.html @@ -0,0 +1,12 @@ +@use super::base_html; + +@(title: &str, content: Content, meta: Content) + +@:base_html(title, { +
+ @:content() +
+ +}, { +@:meta() +}) diff --git a/examples/simple/templates/page/page.rs.html b/examples/simple/templates/page/page.rs.html index d08930b..304178d 100644 --- a/examples/simple/templates/page/page.rs.html +++ b/examples/simple/templates/page/page.rs.html @@ -1,8 +1,8 @@ -@use super::base_html; +@use super::intermediate_html; @(who: &str) -@:base_html(&format!("Hello {who}!"), { +@:intermediate_html(&format!("Hello {who}!"), {

This is page content for @who

}, { diff --git a/examples/warp03/templates/base.rs.html b/examples/warp03/templates/base.rs.html new file mode 100644 index 0000000..66d126e --- /dev/null +++ b/examples/warp03/templates/base.rs.html @@ -0,0 +1,21 @@ +@use super::statics::*; +@use crate::footer; + +@(title: &str, htmlargs: Content, content: Content) + + + + + @title + + + + +
+

@title

+ + @:content() +
+ @:footer() + + diff --git a/examples/warp03/templates/error.rs.html b/examples/warp03/templates/error.rs.html index 8ce6610..c42c68e 100644 --- a/examples/warp03/templates/error.rs.html +++ b/examples/warp03/templates/error.rs.html @@ -1,25 +1,10 @@ -@use super::statics::*; -@use crate::footer; +@use super::intermediate_html; @use warp::http::StatusCode; @(code: StatusCode, message: &str) - - - - - Error @code.as_u16(): @code.canonical_reason().unwrap_or("error") - - - - -
-

@code.canonical_reason().unwrap_or("error")

- -

@message

-

We are sorry about this. - In a real application, this would mention the incident having - been logged, and giving contact details for further reporting.

-
- @:footer() - - +@:intermediate_html(code.canonical_reason().unwrap_or("Error"), { class="error"}, { +

@message

+

We are sorry about this. + In a real application, this would mention the incident having + been logged, and giving contact details for further reporting.

+}) diff --git a/examples/warp03/templates/intermediate.rs.html b/examples/warp03/templates/intermediate.rs.html new file mode 100644 index 0000000..653f79e --- /dev/null +++ b/examples/warp03/templates/intermediate.rs.html @@ -0,0 +1,7 @@ +@use super::base_html; + +@(title: &str, htmlargs: Content, content: Content) +@:base_html(title, htmlargs, { + @:content() +

This whole site is an example.

+}) diff --git a/examples/warp03/templates/page.rs.html b/examples/warp03/templates/page.rs.html index 8dce6e4..cf5f852 100644 --- a/examples/warp03/templates/page.rs.html +++ b/examples/warp03/templates/page.rs.html @@ -1,19 +1,8 @@ @use super::statics::*; -@use crate::footer; +@use super::intermediate_html; @(paras: &[(&str, usize)]) - - - - - Example - - - - -
-

Example

- +@:intermediate_html("Example", {}, {
Squirrel! @@ -30,7 +19,4 @@

Example

This is a @order paragraph. } } -
- @:footer() - - +}) From 47f433790f2d35f440d05bb4dcd995df4798b10c Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Sun, 30 Jun 2024 17:07:12 +0200 Subject: [PATCH 2/2] Workaround for #134. Gave up on finding a real fix for #134, so documenting a workaround instead. --- .../warp03/templates/intermediate.rs.html | 2 +- src/Template_syntax.rs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/examples/warp03/templates/intermediate.rs.html b/examples/warp03/templates/intermediate.rs.html index 653f79e..be3fa56 100644 --- a/examples/warp03/templates/intermediate.rs.html +++ b/examples/warp03/templates/intermediate.rs.html @@ -1,7 +1,7 @@ @use super::base_html; @(title: &str, htmlargs: Content, content: Content) -@:base_html(title, htmlargs, { +@:base_html(title, {@:htmlargs()}, { @:content()

This whole site is an example.

}) diff --git a/src/Template_syntax.rs b/src/Template_syntax.rs index 90fdf70..7fe77ef 100644 --- a/src/Template_syntax.rs +++ b/src/Template_syntax.rs @@ -301,4 +301,26 @@ pub mod d_Calling_other_templates { //!

page content ...

//! }) //! ``` + //! + //! ## Intermediate templates with block parameters + //! + //! Due to a limitation in Ructe, it is currently not possible to + //! take a block parameter and send directly along to further + //! templates. + //! The following will not work: + //! + //! ```compile_fail + //! @(title: &str, body: Content) {{ + //! @:base_page_html(title, body) + //! }} + //! ``` + //! + //! Instead, the parameter needs to be a block, even if only to + //! call the existing one: + //! + //! ```text + //! @(title: &str, body: Content) {{ + //! @:base_page_html(title, {@:body()}) + //! }} + //! ``` }