[go: up one dir, main page]

tera 0.11.10

Template engine based on Jinja2/Django templates
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
mod forloop;
mod square_brackets;
#[cfg(test)]
mod tests;

use std::collections::HashMap;

use serde_json::to_string_pretty;
use serde_json::value::{to_value, Number, Value};
use serde_json::map::Map as JsonMap;

use self::forloop::{ForLoop, ForLoopState};
use self::square_brackets::pull_out_square_bracket;
use parser::ast::*;
use template::Template;
use tera::Tera;
use errors::{Result, ResultExt};
use context::{get_json_pointer, ValueNumber, ValueRender, ValueTruthy};
use utils::escape_html;

static MAGICAL_DUMP_VAR: &'static str = "__tera_context";

#[derive(Debug)]
pub struct Renderer<'a> {
    template: &'a Template,
    context: Value,
    tera: &'a Tera,
    /// All ongoing forloops
    for_loops: Vec<ForLoop>,
    /// Looks like Vec<filename: {macro_name: macro_def}>
    macros: Vec<HashMap<String, &'a HashMap<String, MacroDefinition>>>,
    /// Set when rendering macros, empty if not in a macro. Loops in macros are set
    /// as part of the macro_context and not for_loops
    /// Vec<(MacroContext, Vec<loops>)>
    macro_context: Vec<(Value, Vec<ForLoop>)>,
    /// Keeps track of which macro namespace we're on in order to resolve the `self::` syntax
    macro_namespaces: Vec<String>,
    /// Whether this template should be escaped or not
    should_escape: bool,
    /// Used when super() is used in a block, to know where we are in our stack of
    /// definitions and for which block
    /// Vec<(block name, level)>
    blocks: Vec<(String, usize)>,
}

impl<'a> Renderer<'a> {
    pub fn new(template: &'a Template, tera: &'a Tera, context: Value) -> Renderer<'a> {
        let should_escape = tera.autoescape_suffixes.iter().any(|ext| {
            // We prefer a `path` if set, otherwise use the `name`
            if let Some(ref p) = template.path {
                return p.ends_with(ext);
            }
            template.name.ends_with(ext)
        });

        Renderer {
            template,
            tera,
            context,
            should_escape,
            for_loops: vec![],
            macros: vec![],
            macro_context: vec![],
            macro_namespaces: vec![],
            blocks: vec![],
        }
    }

    /// Lookup a key in the context, taking into account macros and loops
    fn lookup_ident(&mut self, key: &str) -> Result<Value> {
        // Differentiate between macros and general context
        let (context, for_loops) = match self.macro_context.last() {
            Some(c) => (&c.0, &c.1),
            None => (&self.context, &self.for_loops),
        };

        // Magical variable that just dumps the context
        if key == MAGICAL_DUMP_VAR {
            // Unwraps are safe since we are dealing with things that are already Value
            return Ok(to_value(to_string_pretty(context).unwrap()).unwrap());
        }

        /// This will convert a Tera variable to a json pointer if it is possible by replacing
        /// the index with their evaluated stringified value
        #[inline]
        fn evaluate_sub_variable(context: &Value, key: &str, tpl_name: &str) -> Result<String> {
            let sub_vars_to_calc = pull_out_square_bracket(key);
            let mut new_key = key.to_string();

            for sub_var in &sub_vars_to_calc {
                // Translate from variable name to variable value
                match find_variable(context, sub_var.as_ref(), tpl_name) {
                    Err(e) => {
                        bail!(format!("Variable {} can not be evaluated because: {}", key, e));
                    }
                    Ok(post_var) => {
                        let post_var_as_str = match post_var {
                            Value::String(ref s) => s.to_string(),
                            Value::Number(ref n) => n.to_string(),
                            _ => bail!(
                                "Only variables evaluating to String or Number can be used as \
                                index (`{}` of `{}`)",
                                sub_var,
                                key,
                            ),
                        };

                        // Rebuild the original key String replacing variable name with value
                        let nk = new_key.clone();
                        let divider = "[".to_string() + sub_var + "]";
                        let mut the_parts = nk.splitn(2, divider.as_str());

                        new_key = the_parts.next().unwrap().to_string()
                            + "."
                            + post_var_as_str.as_ref()
                            + the_parts.next().unwrap_or("");
                    }
                }
            }
            Ok(
                new_key
                    .replace("['", ".")
                    .replace("[\"", ".")
                    .replace("[", ".")
                    .replace("']", "")
                    .replace("\"]", "")
                    .replace("]", "")
            )
        }

        #[inline]
        fn find_variable(context: &Value, key: &str, tpl_name: &str) -> Result<Value> {
            let key_s = if key.contains('[') {
                evaluate_sub_variable(context, key, tpl_name)?
            } else {
                key.into()
            };

            match context.pointer(&get_json_pointer(key_s.as_ref())) {
                Some(v) => Ok(v.clone()),
                None => bail!(
                    "Variable `{}` not found in context while rendering '{}'",
                    key,
                    tpl_name
                ),
            }
        }

        // Look in the plain context if we aren't in a for loop
        if for_loops.is_empty() {
            return find_variable(context, key, &self.template.name);
        }

        // Separates the initial key (anything before a dot) from everything after
        let (real_key, tail) = if let Some(tail_pos) = key.find('.') {
            (&key[..tail_pos], &key[tail_pos + 1..])
        } else {
            (key, "")
        };

        // The variable might be from a for loop so we start from the most inner one
        for for_loop in for_loops.iter().rev() {
            // 1st case: one of Tera loop built-in variable
            if real_key == "loop" {
                match tail {
                    "index" => {
                        return Ok(to_value(&(for_loop.current + 1))?);
                    }
                    "index0" => {
                        return Ok(to_value(&for_loop.current)?);
                    }
                    "first" => {
                        return Ok(to_value(&(for_loop.current == 0))?);
                    }
                    "last" => {
                        return Ok(to_value(&(for_loop.current == for_loop.len() - 1))?);
                    }
                    _ => bail!("Unknown loop built-in variable: {:?}", key),
                }
            }

            // 2rd case: the variable is the key of a KeyValue for loop
            if for_loop.is_key(key) {
                return Ok(to_value(&for_loop.get_current_key())?);
            }

            // Last case: the variable starts with the value name of the for loop or has been {% set %}
            let value = if real_key == for_loop.value_name {
                for_loop.get_current_value()
            } else {
                for_loop.extra_values.get(real_key)
            };

            if let Some(v) = value {
                if tail.is_empty() {
                    return Ok(v.clone());
                }
                // A struct or some nested structure
                return find_variable(v, tail, &self.template.name)
                    .chain_err(|| format!("Variable lookup failed in forloop for `{}`", key));
            }
        }

        // Gets there when looking a variable in the global context while in a forloop
        find_variable(context, key, &self.template.name)
    }

    fn current_for_loop_mut(&mut self) -> Option<&mut ForLoop> {
        match self.macro_context.last_mut() {
            Some(&mut (_, ref mut for_loops)) => for_loops.last_mut(),
            None => self.for_loops.last_mut(),
        }
    }

    fn current_for_loop(&self) -> Option<&ForLoop> {
        match self.macro_context.last() {
            Some(&(_, ref for_loops)) => for_loops.last(),
            None => self.for_loops.last(),
        }
    }

    /// Inserts the result of the expression in the right context with `key` as the ... key
    fn eval_set(&mut self, set: &Set) -> Result<()> {
        let val = self.safe_eval_expression(&set.value)?;

        let context = match self.macro_context.last_mut() {
            Some(c) => c.0.as_object_mut().unwrap(),
            None => match self.for_loops.last_mut() {
                Some(f) => if set.global {
                    self.context.as_object_mut().unwrap()
                } else {
                    &mut f.extra_values
                },
                None => self.context.as_object_mut().unwrap(),
            },
        };

        context.insert(set.key.to_string(), val);
        Ok(())
    }

    /// In some cases, we will have filters in lhs/rhs of a math expression
    /// `eval_as_number` only works on ExprVal rather than Expr
    fn eval_expr_as_number(&mut self, expr: &Expr) -> Result<Option<Number>> {
        if !expr.filters.is_empty() {
            match self.eval_expression(expr)? {
                Value::Number(s) => Ok(Some(s)),
                _ => bail!("Tried to do math with an expression not resulting in a number"),
            }
        } else {
            self.eval_as_number(&expr.val)
        }
    }

    /// Return the value of an expression as a number
    fn eval_as_number(&mut self, expr: &ExprVal) -> Result<Option<Number>> {
        let result = match *expr {
            ExprVal::Ident(ref ident) => {
                let v = self.lookup_ident(ident)?;
                if v.is_i64() {
                    Some(Number::from(v.as_i64().unwrap()))
                } else if v.is_u64() {
                    Some(Number::from(v.as_u64().unwrap()))
                } else if v.is_f64() {
                    Some(Number::from_f64(v.as_f64().unwrap()).unwrap())
                } else {
                    bail!(
                        "Variable `{}` was used in a math operation but is not a number",
                        ident,
                    )
                }
            },
            ExprVal::Int(val) => Some(Number::from(val)),
            ExprVal::Float(val) => Some(Number::from_f64(val).unwrap()),
            ExprVal::Math(MathExpr { ref lhs, ref rhs, ref operator }) => {
                let (l, r) = match (self.eval_expr_as_number(lhs)?, self.eval_expr_as_number(rhs)?) {
                    (Some(l), Some(r)) => (l, r),
                    _ => return Ok(None),
                };

                match *operator {
                    MathOperator::Mul => {
                        if l.is_i64() && r.is_i64() {
                            let ll = l.as_i64().unwrap();
                            let rr = r.as_i64().unwrap();
                            Some(Number::from(ll * rr))
                        } else if l.is_u64() && r.is_u64() {
                            let ll = l.as_u64().unwrap();
                            let rr = r.as_u64().unwrap();
                            Some(Number::from(ll * rr))
                        } else {
                            let ll = l.as_f64().unwrap();
                            let rr = r.as_f64().unwrap();
                            Some(Number::from_f64(ll * rr).unwrap())
                        }
                    },
                    MathOperator::Div => {
                        let ll = l.as_f64().unwrap();
                        let rr = r.as_f64().unwrap();
                        let res = ll / rr;
                        if res.is_nan() {
                            None
                        } else {
                            Some(Number::from_f64(res).unwrap())
                        }
                    },
                    MathOperator::Add => {
                        if l.is_i64() && r.is_i64() {
                            let ll = l.as_i64().unwrap();
                            let rr = r.as_i64().unwrap();
                            Some(Number::from(ll + rr))
                        } else if l.is_u64() && r.is_u64() {
                            let ll = l.as_u64().unwrap();
                            let rr = r.as_u64().unwrap();
                            Some(Number::from(ll + rr))
                        } else {
                            let ll = l.as_f64().unwrap();
                            let rr = r.as_f64().unwrap();
                            Some(Number::from_f64(ll + rr).unwrap())
                        }
                    },
                    MathOperator::Sub => {
                        if l.is_i64() && r.is_i64() {
                            let ll = l.as_i64().unwrap();
                            let rr = r.as_i64().unwrap();
                            Some(Number::from(ll - rr))
                        } else if l.is_u64() && r.is_u64() {
                            let ll = l.as_u64().unwrap();
                            let rr = r.as_u64().unwrap();
                            Some(Number::from(ll - rr))
                        } else {
                            let ll = l.as_f64().unwrap();
                            let rr = r.as_f64().unwrap();
                            Some(Number::from_f64(ll - rr).unwrap())
                        }
                    },
                    MathOperator::Modulo => {
                        if l.is_i64() && r.is_i64() {
                            let ll = l.as_i64().unwrap();
                            let rr = r.as_i64().unwrap();
                            Some(Number::from(ll % rr))
                        } else if l.is_u64() && r.is_u64() {
                            let ll = l.as_u64().unwrap();
                            let rr = r.as_u64().unwrap();
                            Some(Number::from(ll % rr))
                        } else {
                            let ll = l.as_f64().unwrap();
                            let rr = r.as_f64().unwrap();
                            Some(Number::from_f64(ll % rr).unwrap())
                        }
                    }
                }
            }
            ExprVal::String(ref val) => bail!("Tried to do math with a string: `{}`", val),
            ExprVal::Bool(val) => bail!("Tried to do math with a boolean: `{}`", val),
            _ => unreachable!("unimplemented"),
        };

        Ok(result)
    }

    /// Return the value of an expression as a bool
    fn eval_as_bool(&mut self, expr: &Expr) -> Result<bool> {
        let res = match expr.val {
            ExprVal::Logic(LogicExpr { ref lhs, ref rhs, ref operator }) => {
                match *operator {
                    LogicOperator::Or => self.eval_as_bool(lhs)? || self.eval_as_bool(rhs)?,
                    LogicOperator::And => self.eval_as_bool(lhs)? && self.eval_as_bool(rhs)?,
                    LogicOperator::Gt
                    | LogicOperator::Gte
                    | LogicOperator::Lt
                    | LogicOperator::Lte => {
                        let l = self.eval_expr_as_number(lhs)?;
                        let r = self.eval_expr_as_number(rhs)?;

                        let (ll, rr) = match (l, r) {
                            (Some(nl), Some(nr)) => (nl, nr),
                            _ => bail!("Comparison to NaN")
                        };

                        match *operator {
                            LogicOperator::Gte => ll.as_f64().unwrap() >= rr.as_f64().unwrap(),
                            LogicOperator::Gt => ll.as_f64().unwrap() > rr.as_f64().unwrap(),
                            LogicOperator::Lte => ll.as_f64().unwrap() <= rr.as_f64().unwrap(),
                            LogicOperator::Lt => ll.as_f64().unwrap() < rr.as_f64().unwrap(),
                            _ => unreachable!(),
                        }
                    }
                    LogicOperator::Eq | LogicOperator::NotEq => {
                        let mut lhs_val = self.eval_expression(lhs)?;
                        let mut rhs_val = self.eval_expression(rhs)?;

                        // Monomorphize number vals.
                        if lhs_val.is_number() || rhs_val.is_number() {
                            // We're not implementing JS so can't compare things of different types
                            if !lhs_val.is_number() || !rhs_val.is_number() {
                                return Ok(false);
                            }

                            lhs_val = Value::Number(
                                Number::from_f64(lhs_val.as_f64().unwrap()).unwrap()
                            );
                            rhs_val = Value::Number(
                                Number::from_f64(rhs_val.as_f64().unwrap()).unwrap()
                            );
                        }

                        match *operator {
                            LogicOperator::Eq => lhs_val == rhs_val,
                            LogicOperator::NotEq => lhs_val != rhs_val,
                            _ => unreachable!(),
                        }
                    }
                }
            }
            ExprVal::Ident(ref ident) => {
                self.lookup_ident(ident).map(|v| v.is_truthy()).unwrap_or(false)
            }
            ExprVal::Math(_) | ExprVal::Int(_) | ExprVal::Float(_) => {
                match self.eval_as_number(&expr.val) {
                    Ok(Some(n)) => n.as_f64().unwrap() != 0.0,
                    Ok(None) => false,
                    Err(_) => false,
            	}
            }
            ExprVal::Test(ref test) => self.eval_test(test).unwrap_or(false),
            ExprVal::Bool(val) => val,
            ExprVal::String(ref string) => !string.is_empty(),
            _ => unreachable!("unimplemented logic operation for {:?}", expr),
        };

        if expr.negated {
            return Ok(!res);
        }

        Ok(res)
    }

    fn eval_test(&mut self, test: &Test) -> Result<bool> {
        let tester_fn = self.tera.get_tester(&test.name)?;

        let mut tester_args = vec![];
        for arg in &test.args {
            tester_args.push(self.safe_eval_expression(arg)?);
        }

        Ok(tester_fn(self.lookup_ident(&test.ident).ok(), tester_args)?)
    }

    fn eval_global_fn_call(&mut self, fn_call: &FunctionCall) -> Result<Value> {
        let global_fn = self.tera.get_global_function(&fn_call.name)?;

        let mut args = HashMap::new();
        for (arg_name, expr) in &fn_call.args {
            args.insert(arg_name.to_string(), self.safe_eval_expression(expr)?);
        }

        global_fn(args)
    }

    fn eval_macro_call(&mut self, macro_call: &MacroCall) -> Result<String> {
        // We need to find the active namespace in Tera if `self` is used
        // Since each macro (other than the `self` ones) pushes its own namespace
        // to the stack when being rendered, we can just lookup the last namespace that was pushed
        // to find out the active one
        let active_namespace = match macro_call.namespace.as_ref() {
            "self" => {
                // This happens when calling a macro defined in the file itself without imports
                self.macro_namespaces
                    .last()
                    .expect("Open an issue with a template sample please (mention `self namespace macro`)!")
                    .to_string()
            }
            _ => {
                self.macro_namespaces.push(macro_call.namespace.clone());
                macro_call.namespace.clone()
            }
        };

        // We get our macro definition using the namespace name we just got
        let macro_definition = self.macros
            .last()
            .and_then(|m| m.get(&active_namespace))
            .and_then(|m| m.get(&macro_call.name))
            .ok_or_else(|| {
                format!(
                    "Macro `{}` was not found in the namespace `{}`",
                    macro_call.name, active_namespace,
                )
            })?;

        let mut macro_context = JsonMap::new();

        // First the default arguments
        for (arg_name, default_value) in &macro_definition.args {
            let value = match macro_call.args.get(arg_name) {
                Some(val) => self.safe_eval_expression(val)?,
                None => match *default_value {
                    Some(ref val) => self.safe_eval_expression(val)?,
                    None => bail!(
                        "Macro `{}` is missing the argument `{}`",
                        macro_call.name,
                        arg_name,
                    ),
                }
            };
            macro_context.insert(arg_name.to_string(), value);
        }
        // Push this context to our stack of macro context so the renderer can pick variables
        // from it
        self.macro_context.push((macro_context.into(), vec![]));
        let output = self.render_body(&macro_definition.body)?;
        // If the current namespace wasn't `self`, we remove it since it's not needed anymore
        // In the `self` case, we are still in the parent macro and its namespace is still
        // needed so we keep it
        if macro_call.namespace == active_namespace {
            self.macro_namespaces.pop();
        }
        // We remove the macro context we just rendered from our stack of contexts
        self.macro_context.pop();
        Ok(output)
    }

    fn eval_filter(&mut self, value: Value, filter: &FunctionCall) -> Result<Value> {
        let filter_fn = self.tera.get_filter(&filter.name)?;

        let mut args = HashMap::new();
        for (arg_name, expr) in &filter.args {
            args.insert(arg_name.to_string(), self.safe_eval_expression(expr)?);
        }

        filter_fn(value, args)
    }

    fn eval_expression(&mut self, expr: &Expr) -> Result<Value> {
        let mut needs_escape = false;

        let mut res = match expr.val {
            ExprVal::Array(ref arr) => {
                let mut vals = vec![];
                for v in arr {
                    vals.push(self.eval_expression(v)?);
                }
                Value::Array(vals)
            },
            ExprVal::String(ref val) => {
                needs_escape = true;
                Value::String(val.to_string())
            },
            ExprVal::StringConcat(ref str_concat) => {
                let mut res = String::new();
                for s in &str_concat.values {
                    match *s {
                        ExprVal::String(ref v) => res.push_str(&v),
                        ExprVal::Ident(ref i) => {
                            match self.lookup_ident(i)? {
                                Value::String(v) => res.push_str(&v),
                                _ => bail!("Tried to concat a value that is not a string from ident {}", i)
                            }
                        }
                        _ => unreachable!()
                    };
                }

                Value::String(res)
            },
            ExprVal::Int(val) => Value::Number(val.into()),
            ExprVal::Float(val) => Value::Number(Number::from_f64(val).unwrap()),
            ExprVal::Bool(val) => Value::Bool(val),
            ExprVal::Ident(ref ident) => {
                needs_escape = ident != MAGICAL_DUMP_VAR;
                // Negated idents are special cased as `not undefined_ident` should not
                // error but instead be falsy values
                match self.lookup_ident(ident) {
                    Ok(val) => val,
                    Err(e) => {
                        if expr.has_default_filter() {
                            if let Some(default_expr) = expr.filters[0].args.get("value") {
                                self.eval_expression(default_expr)?
                            } else {
                                bail!("The `default` filter requires a `value` argument.");
                            }
                        } else {
                            if !expr.negated {
                                return Err(e);
                            }
                            // A negative undefined ident is !false so truthy
                            return Ok(Value::Bool(true));
                        }
                    }
                }
            }
            ExprVal::FunctionCall(ref fn_call) => {
                needs_escape = true;
                self.eval_global_fn_call(fn_call)?
            }
            ExprVal::MacroCall(ref macro_call) => Value::String(self.eval_macro_call(macro_call)?),
            ExprVal::Test(ref test) => Value::Bool(self.eval_test(test)?),
            ExprVal::Logic(_) => Value::Bool(self.eval_as_bool(expr)?),
            ExprVal::Math(_) => {
                match self.eval_as_number(&expr.val) {
		    Ok(Some(n)) => Value::Number(n),
		    Ok(None) => Value::String("NaN".to_owned()),
		    Err(e) => bail!(e.to_string()),
		}
            }
            _ => unreachable!("{:?}", expr),
        };

        // Checks if it's a string and we need to escape it (if the first filter is `safe` we don't)
        if self.should_escape
            && needs_escape
            && res.is_string()
            && expr.filters.first().map_or(true, |f| f.name != "safe") {
            res = to_value(self.tera.get_escape_fn()(res.as_str().unwrap()))?;
        }

        for filter in &expr.filters {
            if filter.name == "safe" || filter.name == "default" {
                continue;
            }
            res = self.eval_filter(res, filter)?;
        }

        // Lastly, we need to check if the expression is negated, thus turning it into a bool
        if expr.negated {
            return Ok(Value::Bool(!res.is_truthy()));
        }

        Ok(res)
    }

    /// A wrapper around `eval_expression` that disables escaping before calling it and sets it back
    /// after. Used when evaluating expressions where we never want escaping, such as function
    /// arguments.
    fn safe_eval_expression(&mut self, expr: &Expr) -> Result<Value> {
        let should_escape = self.should_escape;
        self.should_escape = false;
        let res = self.eval_expression(expr);
        self.should_escape = should_escape;
        res
    }

    fn render_if(&mut self, node: &If) -> Result<String> {
        for &(_, ref expr, ref body) in &node.conditions {
            if self.eval_as_bool(expr)? {
                return self.render_body(body);
            }
        }

        if let Some((_, ref body)) = node.otherwise {
            return self.render_body(body);
        }

        Ok(String::new())
    }

    fn render_for(&mut self, node: &Forloop) -> Result<String> {
        let container_name = match node.container.val {
            ExprVal::Ident(ref ident) => ident,
            ExprVal::FunctionCall(FunctionCall { ref name, .. }) => name,
            ExprVal::Array(_) => "an array literal",
            _ => bail!(
                "Forloop containers have to be an ident or a function call (tried to iterate on '{:?}')",
                node.container.val,
            ),
        };

        let container_val = self.safe_eval_expression(&node.container)?;

        let for_loop = match container_val {
            Value::Array(_) => {
                if node.key.is_some() {
                    bail!(
                        "Tried to iterate using key value on variable `{}`, but it isn't an object/map",
                        container_name,
                    );
                }
                ForLoop::new(&node.value, container_val)
            }
            Value::Object(_) => {
                if node.key.is_none() {
                    bail!(
                        "Tried to iterate using key value on variable `{}`, but it is missing a key",
                        container_name,
                    );
                }
                ForLoop::new_key_value(
                    &node.key.clone().unwrap(),
                    &node.value,
                    container_val,
                )
            }
            _ => bail!(
                "Tried to iterate on a container (`{}`) that has a unsupported type",
                container_name,
            ),
        };

        let length = for_loop.len();

        match self.macro_context.last_mut() {
            Some(m) => m.1.push(for_loop),
            None => self.for_loops.push(for_loop),
        };

        let mut output = String::new();

        for _ in 0..length {
            output.push_str(&self.render_body(&node.body)?);

            if self.current_for_loop_mut().unwrap().state == ForLoopState::Break {
                break
            }

            // Safe unwrap
            match self.macro_context.last_mut() {
                Some(m) => m.1.last_mut().unwrap().increment(),
                None => self.for_loops.last_mut().unwrap().increment(),
            };
        }
        // Clean up after ourselves
        match self.macro_context.last_mut() {
            Some(m) => m.1.pop(),
            None => self.for_loops.pop(),
        };

        Ok(output)
    }

    /// Adds the macro for the given template into the renderer and returns
    /// whether it had some macros or not
    /// Used when rendering blocks
    fn import_template_macros(&mut self, tpl_name: &str) -> Result<bool> {
        let tpl = self.tera.get_template(tpl_name)?;
        if tpl.imported_macro_files.is_empty() {
            return Ok(false);
        }

        /// Macro templates can import other macro templates so the macro loading
        /// needs to happen recursively
        /// We need all of the macros loaded in one go to be in the same hashmap
        /// for easy popping as well, otherwise there could be stray macro definitions
        /// remaining
        fn load_macros<'a>(
            tera: &'a Tera,
            tpl: &Template,
        ) -> Result<HashMap<String, &'a HashMap<String, MacroDefinition>>> {
            let mut macros = HashMap::new();

            for &(ref filename, ref namespace) in &tpl.imported_macro_files {
                let macro_tpl = tera.get_template(filename)?;
                macros.insert(namespace.to_string(), &macro_tpl.macros);
                if !macro_tpl.imported_macro_files.is_empty() {
                    macros.extend(load_macros(tera, macro_tpl)?);
                }
            }

            Ok(macros)
        }

        self.macros.push(load_macros(self.tera, tpl)?);

        Ok(true)
    }

    /// The way inheritance work is that the top parent will be rendered by the renderer so for blocks
    /// we want to look from the bottom (`level = 0`, the template the user is actually rendering)
    /// to the top (the base template).
    /// If we are rendering a block,
    fn render_block(&mut self, block: &Block, level: usize) -> Result<String> {
        let blocks_definitions = match level {
            // look for the template we're currently rendering
            0 => &self.template.blocks_definitions,
            // or look at its parents
            _ => {
                &self.tera
                    .get_template(&self.template.parents[level - 1])
                    .unwrap()
                    .blocks_definitions
            },
        };

        // Can we find this one block in these definitions? If so render it
        if let Some(block_def) = blocks_definitions.get(&block.name) {
            let (ref tpl_name, Block { ref body, .. }) = block_def[0];
            self.blocks.push((block.name.to_string(), level));
            let has_macro = self.import_template_macros(tpl_name)?;
            let res = self.render_body(body);
            if has_macro {
                self.macros.pop();
            }
            return res;
        }

        // Do we have more parents to look through?
        if level < self.template.parents.len() {
            return self.render_block(block, level + 1);
        }

        // Nope, just render the body we got
        self.render_body(&block.body)
    }

    /// Only called while rendering a block.
    /// This will look up the block we are currently rendering and its level and try to render
    /// the block at level + n, where would be the next template in the hierarchy the block is present
    fn do_super(&mut self) -> Result<String> {
        let (block_name, level) = self.blocks.pop().unwrap();
        let mut next_level = level + 1;

        while next_level <= self.template.parents.len() {
            let blocks_definitions = &self.tera
                .get_template(&self.template.parents[next_level - 1])
                .unwrap()
                .blocks_definitions;

            if let Some(block_def) = blocks_definitions.get(&block_name) {
                let (ref tpl_name, Block { ref body, .. }) = block_def[0];
                self.blocks.push((block_name.to_string(), next_level));
                let has_macro = self.import_template_macros(tpl_name)?;
                let res = self.render_body(body);
                if has_macro {
                    self.macros.pop();
                }
                // Can't go any higher for that block anymore?
                if next_level >= self.template.parents.len() {
                    // then remove it from the stack, we're done with it
                    self.blocks.pop();
                }
                return res;
            } else {
                next_level += 1;
            }
        }

        bail!("Tried to use super() in the top level block")
    }

    fn render_node(&mut self, node: &Node) -> Result<String> {
        let output = match *node {
            Node::Text(ref s) | Node::Raw(_, ref s, _) => s.to_string(),
            Node::VariableBlock(ref expr) => self.eval_expression(expr)?.render(),
            Node::Set(_, ref set) => self.eval_set(set).and(Ok(String::new()))?,
            Node::FilterSection(_, FilterSection { ref filter, ref body }, _) => {
                let output = self.render_body(body)?;

                self.eval_filter(Value::String(output), filter)?.render()
            }
            // Macros have been imported at the beginning
            Node::ImportMacro(_, _, _) => String::new(),
            Node::If(ref if_node, _) => self.render_if(if_node)?,
            Node::Forloop(_, ref forloop, _) => self.render_for(forloop)?,
            Node::Break(_) => {
                self.current_for_loop_mut().unwrap().break_loop();
                String::new()
            },
            Node::Continue(_) => {
                self.current_for_loop_mut().unwrap().continue_loop();
                String::new()
            },
            Node::Block(_, ref block, _) => self.render_block(block, 0)?,
            Node::Super => self.do_super()?,
            Node::Include(_, ref tpl_name) => {
                let has_macro = self.import_template_macros(tpl_name)?;
                let res = self.render_body(&self.tera.get_template(tpl_name)?.ast);
                if has_macro {
                    self.macros.pop();
                }
                return res;
            }
            _ => unreachable!("render_node -> unexpected node: {:?}", node),
        };

        Ok(output)
    }

    fn render_body(&mut self, body: &[Node]) -> Result<String> {
        let mut output = String::new();

        for n in body {
            output.push_str(&self.render_node(n)?);
            if let Some(for_loop) = self.current_for_loop() {
                match for_loop.state {
                    ForLoopState::Continue
                    | ForLoopState::Break => break,
                    ForLoopState::Normal => {},
                }
            }
        }

        Ok(output)
    }

    // Helper fn that tries to find the current context: are we in a macro? in a parent template?
    // in order to give the best possible error when getting an error when rendering a tpl
    fn get_error_location(&self) -> String {
        let mut error_location = format!("Failed to render '{}'", self.template.name);

        // in a macro?
        if let Some(macro_namespace) = self.macro_namespaces.last() {
            error_location += &format!(
                ": error while rendering a macro from the `{}` namespace",
                macro_namespace,
            );
        }

        // which template are we in?
        if let Some(&(ref name, ref level)) = self.blocks.last() {
            let block_def = self.template
                .blocks_definitions
                .get(name)
                .and_then(|b| b.get(*level));

            if let Some(&(ref tpl_name, _)) = block_def {
                if tpl_name != &self.template.name {
                    error_location += &format!(" (error happened in '{}').", tpl_name);
                }
            } else {
                error_location += " (error happened in a parent template)";
            }
        } else if let Some(parent) = self.template.parents.last() {
            // Error happened in the base template, outside of blocks
            error_location += &format!(" (error happened in '{}').", parent);
        }

        error_location
    }

    pub fn render(&mut self) -> Result<String> {
        // If we have a parent for the template, we start by rendering
        // the one at the top
        let (tpl_name, ast) = match self.template.parents.last() {
            // this unwrap is safe; Tera would have errored already if the template didn't exist
            Some(parent_tpl_name) => {
                let tpl = self.tera.get_template(parent_tpl_name).unwrap();
                (&tpl.name, &tpl.ast)
            }
            None => (&self.template.name, &self.template.ast),
        };

        self.import_template_macros(tpl_name)?;

        let mut output = String::new();
        for node in ast {
            output.push_str(
                &self.render_node(node).chain_err(|| self.get_error_location())?
            );
        }

        Ok(output)
    }
}