1 use crate::context
::Context
;
4 use super::NestedObject
;
8 let mut tera
= Tera
::default();
9 tera
.add_raw_templates(vec
![
10 ("macros", "{% macro hello()%}Hello{% endmacro hello %}"),
13 "{% import \"macros\" as macros %}{% block hey %}{{macros::hello()}}{% endblock hey %}",
18 let result
= tera
.render("tpl", &Context
::new());
20 assert_eq
!(result
.unwrap(), "Hello".to_string());
24 fn render_macros_expression_arg() {
25 let mut context
= Context
::new();
26 context
.insert("pages", &vec
![1, 2, 3, 4, 5]);
27 let mut tera
= Tera
::default();
28 tera
.add_raw_templates(vec
![
29 ("macros", "{% macro hello(val)%}{{val}}{% endmacro hello %}"),
30 ("tpl", "{% import \"macros\" as macros %}{{macros::hello(val=pages|last)}}"),
34 let result
= tera
.render("tpl", &context
);
36 assert_eq
!(result
.unwrap(), "5".to_string());
40 fn render_macros_in_child_templates_same_namespace() {
41 let mut tera
= Tera
::default();
42 tera
.add_raw_templates(vec
![
43 ("grandparent", "{% block hey %}hello{% endblock hey %}"),
44 ("macros", "{% macro hello()%}Hello{% endmacro hello %}"),
45 ("macros2", "{% macro hi()%}Hi{% endmacro hi %}"),
46 ("parent", "{% extends \"grandparent\" %}{% import \"macros\" as macros %}{% block hey %}{{macros::hello()}}{% endblock hey %}"),
47 ("child", "{% extends \"parent\" %}{% import \"macros2\" as macros %}{% block hey %}{{super()}}/{{macros::hi()}}{% endblock hey %}"),
50 let result
= tera
.render("child", &Context
::new());
52 assert_eq
!(result
.unwrap(), "Hello/Hi".to_string());
56 fn render_macros_in_child_templates_different_namespace() {
57 let mut tera
= Tera
::default();
58 tera
.add_raw_templates(vec
![
59 ("grandparent", "{% block hey %}hello{% endblock hey %}"),
60 ("macros", "{% macro hello()%}Hello{% endmacro hello %}"),
61 ("macros2", "{% macro hi()%}Hi{% endmacro hi %}"),
62 ("parent", "{% extends \"grandparent\" %}{% import \"macros\" as macros %}{% block hey %}{{macros::hello()}}{% endblock hey %}"),
63 ("child", "{% extends \"parent\" %}{% import \"macros2\" as macros2 %}{% block hey %}{{super()}}/{{macros2::hi()}}{% endblock hey %}"),
66 let result
= tera
.render("child", &Context
::new());
68 assert_eq
!(result
.unwrap(), "Hello/Hi".to_string());
72 fn render_macros_in_parent_template_with_inheritance() {
73 let mut tera
= Tera
::default();
74 tera
.add_raw_templates(vec
![
75 ("macros", "{% macro hello()%}Hello{% endmacro hello %}"),
76 ("grandparent", "{% import \"macros\" as macros %}{% block hey %}{{macros::hello()}}{% endblock hey %}"),
77 ("child", "{% extends \"grandparent\" %}{% import \"macros\" as macros %}{% block hey %}{{super()}}/{{macros::hello()}}{% endblock hey %}"),
80 let result
= tera
.render("child", &Context
::new());
82 assert_eq
!(result
.unwrap(), "Hello/Hello".to_string());
86 fn macro_param_arent_escaped() {
87 let mut tera
= Tera
::default();
88 tera
.add_raw_templates(vec
![
89 ("macros.html", r
#"{% macro print(val) %}{{val|safe}}{% endmacro print %}"#),
90 ("hello.html", r
#"{% import "macros.html" as macros %}{{ macros::print(val=my_var)}}"#),
93 let mut context
= Context
::new();
94 context
.insert("my_var", &"&");
95 let result
= tera
.render("hello.html", &context
);
97 assert_eq
!(result
.unwrap(), "&".to_string());
101 fn render_set_tag_macro() {
102 let mut tera
= Tera
::default();
103 tera
.add_raw_templates(vec
![
104 ("macros", "{% macro hello()%}Hello{% endmacro hello %}"),
107 "{% import \"macros\" as macros %}{% set my_var = macros::hello() %}{{my_var}}",
111 let result
= tera
.render("hello.html", &Context
::new());
113 assert_eq
!(result
.unwrap(), "Hello".to_string());
117 fn render_macros_with_default_args() {
118 let mut tera
= Tera
::default();
119 tera
.add_raw_templates(vec
![
120 ("macros", "{% macro hello(val=1) %}{{val}}{% endmacro hello %}"),
121 ("hello.html", "{% import \"macros\" as macros %}{{macros::hello()}}"),
124 let result
= tera
.render("hello.html", &Context
::new());
126 assert_eq
!(result
.unwrap(), "1".to_string());
130 fn render_macros_override_default_args() {
131 let mut tera
= Tera
::default();
132 tera
.add_raw_templates(vec
![
133 ("macros", "{% macro hello(val=1) %}{{val}}{% endmacro hello %}"),
134 ("hello.html", "{% import \"macros\" as macros %}{{macros::hello(val=2)}}"),
137 let result
= tera
.render("hello.html", &Context
::new());
139 assert_eq
!(result
.unwrap(), "2".to_string());
143 fn render_recursive_macro() {
144 let mut tera
= Tera
::default();
145 tera
.add_raw_templates(vec
![
148 "{% macro factorial(n) %}{% if n > 1 %}{{ n }} - {{ self::factorial(n=n-1) }}{% else %}1{% endif %}{{ n }}{% endmacro factorial %}",
150 ("hello.html", "{% import \"macros\" as macros %}{{macros::factorial(n=7)}}"),
152 let result
= tera
.render("hello.html", &Context
::new());
154 assert_eq
!(result
.unwrap(), "7 - 6 - 5 - 4 - 3 - 2 - 11234567".to_string());
157 // https://github.com/Keats/tera/issues/202
159 fn recursive_macro_with_loops() {
160 let parent
= NestedObject { label: "Parent".to_string(), parent: None, numbers: vec![1, 2, 3] }
;
161 let child
= NestedObject
{
162 label
: "Child".to_string(),
163 parent
: Some(Box
::new(parent
)),
164 numbers
: vec
![1, 2, 3],
166 let mut context
= Context
::new();
167 context
.insert("objects", &vec
![child
]);
168 let mut tera
= Tera
::default();
170 tera
.add_raw_templates(vec
![
174 {% macro label_for(obj, sep) -%}
175 {%- if obj.parent -%}
176 {{ self::label_for(obj=obj.parent, sep=sep) }}{{sep}}
179 {%- for i in obj.numbers -%}{{ i }}{%- endfor -%}
180 {%- endmacro label_for %}
186 {%- import "macros.html" as macros -%}
187 {%- for obj in objects -%}
188 {{ macros::label_for(obj=obj, sep="|") }}
195 let result
= tera
.render("recursive", &context
);
197 assert_eq
!(result
.unwrap(), "Parent123|Child123".to_string());
200 // https://github.com/Keats/tera/issues/250
202 fn render_macros_in_included() {
203 let mut tera
= Tera
::default();
204 tera
.add_raw_templates(vec
![
205 ("macros", "{% macro my_macro() %}my macro{% endmacro %}"),
206 ("includeme", r
#"{% import "macros" as macros %}{{ macros::my_macro() }}"#),
207 ("example", r
#"{% include "includeme" %}"#),
210 let result
= tera
.render("example", &Context
::new());
212 assert_eq
!(result
.unwrap(), "my macro".to_string());
215 // https://github.com/Keats/tera/issues/255
217 fn import_macros_into_other_macro_files() {
218 let mut tera
= Tera
::default();
219 tera
.add_raw_templates(vec
![
220 ("submacros", "{% macro test() %}Success!{% endmacro %}"),
223 r
#"{% import "submacros" as sub %}{% macro test() %}{{ sub::test() }}{% endmacro %}"#,
225 ("index", r
#"{% import "macros" as macros %}{{ macros::test() }}"#),
228 let result
= tera
.render("index", &Context
::new());
230 assert_eq
!(result
.unwrap(), "Success!".to_string());
234 fn can_load_parent_macro_in_child() {
235 let mut tera
= Tera
::default();
236 tera
.add_raw_templates(vec
![
237 ("macros", "{% macro hello()%}{{ 1 }}{% endmacro hello %}"),
238 ("parent", "{% import \"macros\" as macros %}{{ macros::hello() }}{% block bob %}{% endblock bob %}"),
239 ("child", "{% extends \"parent\" %}{% block bob %}{{ super() }}Hey{% endblock bob %}"),
242 let result
= tera
.render("child", &Context
::new());
244 assert_eq
!(result
.unwrap(), "1Hey".to_string());
248 fn can_load_macro_in_child() {
249 let mut tera
= Tera
::default();
250 tera
.add_raw_templates(vec
![
251 ("macros", "{% macro hello()%}{{ 1 }}{% endmacro hello %}"),
252 ("parent", "{% block bob %}{% endblock bob %}"),
253 ("child", "{% extends \"parent\" %}{% import \"macros\" as macros %}{% block bob %}{{ macros::hello() }}{% endblock bob %}"),
256 let result
= tera
.render("child", &Context
::new());
258 assert_eq
!(result
.unwrap(), "1".to_string());
261 // https://github.com/Keats/tera/issues/333
262 // this test fails in 0.11.14, worked in 0.11.10
264 fn can_inherit_macro_import_from_parent() {
265 let mut tera
= Tera
::default();
266 tera
.add_raw_templates(vec
![
267 ("macros", "{% macro hello()%}HELLO{% endmacro hello %}"),
268 ("parent", "{% import \"macros\" as macros %}{% block bob %}parent{% endblock bob %}"),
269 ("child", "{% extends \"parent\" %}{% block bob %}{{macros::hello()}}{% endblock bob %}"),
273 let result
= tera
.render("child", &Context
::default());
274 assert_eq
!(result
.unwrap(), "HELLO".to_string());
278 fn can_inherit_macro_import_from_grandparent() {
279 let mut tera
= Tera
::default();
280 tera
.add_raw_templates(vec
![
281 ("macros", "{% macro hello()%}HELLO{% endmacro hello %}"),
282 ("grandparent", "{% import \"macros\" as macros %}{% block bob %}grandparent{% endblock bob %}"),
283 ("parent", "{% extends \"grandparent\" %}{% import \"macros\" as macros2 %}{% block bob %}parent{% endblock bob %}"),
284 ("child", "{% extends \"parent\" %}{% block bob %}{{macros::hello()}}-{{macros2::hello()}}{% endblock bob %}"),
287 let result
= tera
.render("child", &Context
::default());
288 assert_eq
!(result
.unwrap(), "HELLO-HELLO".to_string());
292 fn can_load_macro_in_parent_with_grandparent() {
293 let mut tera
= Tera
::default();
294 tera
.add_raw_templates(vec
![
295 ("macros", "{% macro hello()%}{{ 1 }}{% endmacro hello %}"),
296 ("grandparent", "{% block bob %}{% endblock bob %}"),
297 ("parent", "{% extends \"grandparent\" %}{% import \"macros\" as macros %}{% block bob %}{{ macros::hello() }} - Hey{% endblock bob %}"),
298 ("child", "{% extends \"parent\" %}{% block bob %}{{ super() }}{% endblock bob %}"),
301 let result
= tera
.render("child", &Context
::new());
303 assert_eq
!(result
.unwrap(), "1 - Hey".to_string());
307 fn macro_can_load_macro_from_macro_files() {
308 let mut tera
= Tera
::default();
309 tera
.add_raw_templates(vec
![
310 ("submacros", "{% macro emma() %}Emma{% endmacro emma %}"),
311 ("macros", "{% import \"submacros\" as submacros %}{% macro hommage() %}{{ submacros::emma() }} was an amazing person!{% endmacro hommage %}"),
312 ("parent", "{% block main %}Someone was a terrible person!{% endblock main %} Don't you think?"),
313 ("child", "{% extends \"parent\" %}{% import \"macros\" as macros %}{% block main %}{{ macros::hommage() }}{% endblock main %}")
316 let result
= tera
.render("child", &Context
::new());
317 //println!("{:#?}", result);
318 assert_eq
!(result
.unwrap(), "Emma was an amazing person! Don't you think?".to_string());
322 fn macro_can_access_global_context() {
323 let mut tera
= Tera
::default();
324 tera
.add_raw_templates(vec
![
325 ("parent", r
#"{% import "macros" as macros %}{{ macros::test_global() }}"#),
326 ("macros", r
#"{% macro test_global() %}{% set_global value1 = "42" %}{% for i in range(end=1) %}{% set_global value2 = " is the truth." %}{% endfor %}{{ value1 }}{% endmacro test_global %}"#)
329 let result
= tera
.render("parent", &Context
::new());
330 assert_eq
!(result
.unwrap(), "42".to_string());
334 fn template_cant_access_macros_context() {
335 let mut tera
= Tera
::default();
336 tera
.add_raw_templates(vec
![
337 ("parent", r
#"{% import "macros" as macros %}{{ macros::empty() }}{{ quote | default(value="I'd rather have roses on my table than diamonds on my neck.") }}"#),
338 ("macros", r
#"{% macro empty() %}{% set_global quote = "This should not reachable from the calling template!" %}{% endmacro empty %}"#)
341 let result
= tera
.render("parent", &Context
::new());
342 assert_eq
!(result
.unwrap(), "I'd rather have roses on my table than diamonds on my neck.");
346 fn parent_macro_cant_access_child_macro_context() {
347 let mut tera
= Tera
::default();
348 tera
.add_raw_templates(vec
![
349 ("parent", "{% import \"macros\" as macros %}{{ macros::test_global() }}"),
350 ("macros", r
#"{% import "moremacros" as moremacros %}{% macro test_global() %}{% set_global value1 = "ACAB" %}{{ moremacros::another_one() }}{{ value1 }}-{{ value2 | default(value="ACAB") }}{% endmacro test_global %}"#),
351 ("moremacros", r
#"{% macro another_one() %}{% set_global value2 = "1312" %}{% endmacro another_one %}"#)
354 let result
= tera
.render("parent", &Context
::new());
355 assert_eq
!(result
.unwrap(), "ACAB-ACAB".to_string());