aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-02-05 15:00:26 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-02-05 15:00:26 +0100
commit78782783102e069862de005112fe1a2fbb5baea1 (patch)
treeab58e9cb211a6df34b35742847c7db4dfe3fcaf8
parenta4a575f05c7b680ba694d88bfb4f6aa5cc031f13 (diff)
downloadaskama-78782783102e069862de005112fe1a2fbb5baea1.tar.gz
askama-78782783102e069862de005112fe1a2fbb5baea1.tar.bz2
askama-78782783102e069862de005112fe1a2fbb5baea1.zip
Add parser support for block and extend blocks
Diffstat (limited to '')
-rw-r--r--askama/src/generator.rs8
-rw-r--r--askama/src/parser.rs25
2 files changed, 32 insertions, 1 deletions
diff --git a/askama/src/generator.rs b/askama/src/generator.rs
index 22931c7..0f5ec85 100644
--- a/askama/src/generator.rs
+++ b/askama/src/generator.rs
@@ -159,6 +159,10 @@ impl Generator {
}
}
+ fn write_block(&mut self, name: &str) {
+ self.writeln(&format!("self.render_block_{}_into(writer);", name));
+ }
+
fn handle(&mut self, nodes: &Vec<Node>) {
for n in nodes {
match n {
@@ -168,6 +172,10 @@ impl Generator {
&Node::Loop(ref var, ref iter, ref body) => {
self.write_loop(&var, &iter, &body);
},
+ &Node::Block(ref name) => { self.write_block(name) },
+ &Node::Extends(_) | &Node::BlockDef(_, _) => {
+ panic!("no extends or block definition allowed in content");
+ },
}
}
}
diff --git a/askama/src/parser.rs b/askama/src/parser.rs
index 3448330..b642b00 100644
--- a/askama/src/parser.rs
+++ b/askama/src/parser.rs
@@ -17,6 +17,9 @@ pub enum Node<'a> {
Expr(Expr<'a>),
Cond(Vec<(Option<Expr<'a>>, Vec<Node<'a>>)>),
Loop(Target<'a>, Expr<'a>, Vec<Node<'a>>),
+ Extends(Expr<'a>),
+ BlockDef(&'a str, Vec<Node<'a>>),
+ Block(&'a str),
}
pub type Nodes<'a> = Vec<Node<'a>>;
@@ -133,11 +136,31 @@ named!(block_for<Node>, do_parse!(
tag_s!("%}") >>
(Node::Loop(var, iter, block))));
+named!(block_extends<Node>, do_parse!(
+ tag_s!("{%") >>
+ ws!(tag_s!("extends")) >>
+ name: ws!(expr_str_lit) >>
+ tag_s!("%}") >>
+ (Node::Extends(name))));
+
+named!(block_block<Node>, do_parse!(
+ tag_s!("{%") >>
+ ws!(tag_s!("block")) >>
+ name: ws!(nom::alphanumeric) >>
+ tag_s!("%}") >>
+ contents: parse_template >>
+ tag_s!("{%") >>
+ ws!(tag_s!("endblock")) >>
+ tag_s!("%}") >>
+ (Node::BlockDef(str::from_utf8(name).unwrap(), contents))));
+
named!(parse_template<Nodes>, many0!(alt!(
take_content |
expr_node |
block_if |
- block_for)));
+ block_for |
+ block_extends |
+ block_block)));
pub fn parse<'a>(src: &'a str) -> Nodes {
match parse_template(src.as_bytes()) {