Skip to content

Commit

Permalink
Support for fully included templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaz87 committed Feb 1, 2019
1 parent 6b42044 commit e4a4ef9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/renderer/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub struct Processor<'a> {
template_root: &'a Template,
/// The Tera object with template details
tera: &'a Tera,
/// Context of the processor, to share with included processors
context: &'a Value,
/// The call stack for processing
call_stack: CallStack<'a>,
/// The macros organised by template and namespaces
Expand Down Expand Up @@ -134,6 +136,7 @@ impl<'a> Processor<'a> {
template_root,
tera,
call_stack,
context,
macros: MacroCollection::from_original_template(&template, &tera),
should_escape,
blocks: Vec::new(),
Expand Down Expand Up @@ -765,10 +768,15 @@ impl<'a> Processor<'a> {
Node::Super => buffer.push_str(&self.do_super()?),
Node::Include(_, ref tpl_name) => {
let template = self.tera.get_template(tpl_name)?;
self.macros.add_macros_from_template(&self.tera, template)?;
self.call_stack.push_include_frame(tpl_name, template);
let result = self.render_body(&template.ast)?;
self.call_stack.pop();
let result = {
let mut processor = Processor::new(
template,
self.tera,
&self.context,
self.should_escape
);
processor.render()?
};
buffer.push_str(&result);
}
_ => unreachable!("render_node -> unexpected node: {:?}", node),
Expand Down
14 changes: 14 additions & 0 deletions src/renderer/tests/inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,17 @@ fn render_super_in_grandchild_without_redefining_in_parent_works() {
let result = tera.render("child", &Context::new());
assert_eq!(result.unwrap(), "Title - More".to_string());
}

#[test]
fn render_inheritance_in_included_template() {
let mut tera = Tera::default();
tera.add_raw_templates(vec![
("base", "Base - {% include \"child\" %}"),
("parent", "{% block title %}Parent{% endblock %}"),
("child", "{% extends \"parent\" %}{% block title %}{{ super() }} - Child{% endblock %}"),
])
.unwrap();

let result = tera.render("base", &Context::new());
assert_eq!(result.unwrap(), "Base - Parent - Child".to_string());
}

0 comments on commit e4a4ef9

Please sign in to comment.