Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use encoder::Error;
use super::{Data, to_data};

/// `MapBuilder` is a helper type that construct `Data` types.
#[derive(Default)]
#[derive(Default, Debug)]
pub struct MapBuilder {
data: HashMap<String, Data>,
}
Expand Down Expand Up @@ -149,6 +149,15 @@ impl MapBuilder {
data.insert(key.to_string(), Data::Fun(RefCell::new(Box::new(f))));
MapBuilder { data: data }
}
#[inline]
pub fn insert_fn2<K: ToString, F>(self, key: K, f: F) -> MapBuilder
where
F: FnMut(String, &mut (FnMut(String) -> String)) -> String + Send + 'static,
{
let MapBuilder { mut data } = self;
data.insert(key.to_string(), Data::Fun2(RefCell::new(Box::new(f))));
MapBuilder { data: data }
}

/// Return the built `Data`.
#[inline]
Expand Down
4 changes: 3 additions & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum Data {
Vec(Vec<Data>),
Map(HashMap<String, Data>),
Fun(RefCell<Box<FnMut(String) -> String + Send>>),
Fun2(RefCell<Box<FnMut(String, &mut (FnMut(String) -> String)) -> String + Send>>),
}

impl PartialEq for Data {
Expand All @@ -35,6 +36,7 @@ impl fmt::Debug for Data {
Data::Vec(ref v) => write!(f, "VecVal({:?})", v),
Data::Map(ref v) => write!(f, "Map({:?})", v),
Data::Fun(_) => write!(f, "Fun(...)"),
Data::Fun2(_) => write!(f, "Fun2(...)"),
}
}
}
}
46 changes: 46 additions & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct RenderContext<'a> {
template: &'a Template,
indent: String,
line_start: bool,
inner_fn2_rendered: bool,
}

impl<'a> RenderContext<'a> {
Expand All @@ -73,6 +74,7 @@ impl<'a> RenderContext<'a> {
template: template,
indent: "".to_string(),
line_start: true,
inner_fn2_rendered: true,
}
}

Expand Down Expand Up @@ -291,6 +293,50 @@ impl<'a> RenderContext<'a> {
let tokens = try!(self.render_fun(src, otag, ctag, f));
try!(self.render(wr, stack, &tokens));
}
Data::Fun2(ref fcell) => {
let f = &mut *fcell.borrow_mut();
let ctx2 = self.template.ctx.clone();
let partials2 = self.template.partials.clone();
self.inner_fn2_rendered = false;

let mut f0 = {
|src: String| {
let compiler = Compiler::new_with(
ctx2.clone(),
src.chars(),
partials2.clone(),
otag.to_string(),
ctag.to_string(),
);
let mut vw: Vec<u8> = vec![];
match compiler.compile() {
Ok((tokens, _)) => {
/* a trick to reset the flag is to pass an empty string */
self.inner_fn2_rendered = (src.len() > 0);
self.render(&mut vw, stack, &tokens).unwrap_or(())
}
_ => (),
}
String::from_utf8_lossy(&vw).to_string()
}
};
let src = f(src.to_string(), &mut f0);
if self.inner_fn2_rendered {
self.inner_fn2_rendered = false;
try!(self.render_text(wr, &src));
} else {
let compiler = Compiler::new_with(
self.template.ctx.clone(),
src.chars(),
self.template.partials.clone(),
otag.to_string(),
ctag.to_string(),
);

let (tokens, _) = try!(compiler.compile());
try!(self.render(wr, stack, &tokens));
}
}
}
}
};
Expand Down