From 5cad82f38e800a42717284f20e7e0923add1e32f Mon Sep 17 00:00:00 2001 From: max Date: Mon, 11 Dec 2023 16:43:16 +0200 Subject: Allow included templates to `extend`, `import`, and `macro` Signed-off-by: max --- testing/templates/include-extends-base.html | 6 ++++ testing/templates/include-extends-included.html | 2 ++ testing/templates/include-extends.html | 4 +++ testing/templates/include-macro.html | 4 +++ testing/templates/included-macro.html | 6 ++++ testing/tests/include.rs | 41 +++++++++++++++++++++++++ 6 files changed, 63 insertions(+) create mode 100644 testing/templates/include-extends-base.html create mode 100644 testing/templates/include-extends-included.html create mode 100644 testing/templates/include-extends.html create mode 100644 testing/templates/include-macro.html create mode 100644 testing/templates/included-macro.html (limited to 'testing') diff --git a/testing/templates/include-extends-base.html b/testing/templates/include-extends-base.html new file mode 100644 index 0000000..7a54ca0 --- /dev/null +++ b/testing/templates/include-extends-base.html @@ -0,0 +1,6 @@ +
+

Below me is the header

+ {% block header %}{% endblock %} +

Above me is the header

+
+Hello, {{ name }}! diff --git a/testing/templates/include-extends-included.html b/testing/templates/include-extends-included.html new file mode 100644 index 0000000..03b7553 --- /dev/null +++ b/testing/templates/include-extends-included.html @@ -0,0 +1,2 @@ +{% extends "include-extends-base.html" %} +{% block header %}foo{% endblock %} diff --git a/testing/templates/include-extends.html b/testing/templates/include-extends.html new file mode 100644 index 0000000..371c133 --- /dev/null +++ b/testing/templates/include-extends.html @@ -0,0 +1,4 @@ +
+

Welcome

+ {% include "include-extends-included.html" %} +
diff --git a/testing/templates/include-macro.html b/testing/templates/include-macro.html new file mode 100644 index 0000000..e29789d --- /dev/null +++ b/testing/templates/include-macro.html @@ -0,0 +1,4 @@ +{% macro m(name) -%} + Hello, {{ name }}! +{%- endmacro -%} +{% include "included-macro.html" %} diff --git a/testing/templates/included-macro.html b/testing/templates/included-macro.html new file mode 100644 index 0000000..efbae18 --- /dev/null +++ b/testing/templates/included-macro.html @@ -0,0 +1,6 @@ +{% macro m2(name) -%} + Howdy, {{ name }}! +{%- endmacro -%} + +{% call m(name) %} +{% call m2(name2) %} diff --git a/testing/tests/include.rs b/testing/tests/include.rs index f461a7b..c11d96f 100644 --- a/testing/tests/include.rs +++ b/testing/tests/include.rs @@ -12,3 +12,44 @@ fn test_include() { let s = IncludeTemplate { strs: &strs }; assert_eq!(s.render().unwrap(), "\n INCLUDED: foo\n INCLUDED: bar") } + +#[derive(Template)] +#[template(path = "include-extends.html")] +struct IncludeExtendsTemplate<'a> { + name: &'a str, +} + +#[test] +fn test_include_extends() { + let template = IncludeExtendsTemplate { name: "Alice" }; + + assert_eq!( + template.render().unwrap(), + "
\n \ +

Welcome

\n \ +
\n \ +

Below me is the header

\n \ + foo\n \ +

Above me is the header

\n\ +
\n\ + Hello, Alice!\n\ +
" + ); +} + +#[derive(Template)] +#[template(path = "include-macro.html")] +struct IncludeMacroTemplate<'a> { + name: &'a str, + name2: &'a str, +} + +#[test] +fn test_include_macro() { + let template = IncludeMacroTemplate { + name: "Alice", + name2: "Bob", + }; + + assert_eq!(template.render().unwrap(), "Hello, Alice!\nHowdy, Bob!"); +} -- cgit