Is it possible to use loops inside a Rust Mustache (https://github.com/erickt/rust-mustache) MapBuilder to populate the contents of the vector? I've attached a semi-working example, it works with the struct version but not with the builder version.
Edit: The code compiles now thanks to Paul. I was missing two things: |mut builder| and builder = builder.push_map.
The thing is I do not want to use the struct version as I'll need to iterate over the images vector and mutate an item based on an certain condition (and I don't want to change the original vector).
I've looked here https://github.com/erickt/rust-mustache/blob/master/src/builder.rs but none of these code snippets show a loop example. Either it is not possible or I'm doing something wrong. I also created an issue but the erickt doesn't seem very active there.
Thank you.
main.rs:
extern crate serialize;
// [dependencies.rust-mustache]
//
// # git = "https://github.com/erickt/rust-mustache.git"
// git = "https://github.com/tsurai/rust-mustache.git"
extern crate mustache;
mod with_struct;
mod with_builder;
fn main() {
with_struct::go();
with_builder::go();
}
with_builder.rs:
use std::io;
use mustache;
use mustache::MapBuilder;
struct Image {
name: String,
file: String
}
impl Image {
fn new(name: &str, file: &str) -> Image {
Image {
name: String::from_str(name),
file: String::from_str(file)
}
}
}
pub fn go() {
let images = load_images();
let template = mustache::compile_str(template());
let data = MapBuilder::new()
.insert_vec("images", |mut builder| {
// ^~~~~ Need mutableb builder
for image in images.iter() {
builder = builder.push_map(|builder| {
// ^~~~~~~~~~^~~~~ Need to re-assign the builder
builder
.insert_str("name", image.name.clone())
.insert_str("file", image.file.clone())
});
}
builder
// ^~~~~ Can now return it
})
.build();
let _ = template.render_data(&mut io::stdout(), &data);
}
fn template<'a>() -> &'a str {
"
<ul>
{{#images}}
<li>
<a href=\"{{file}}\">{{name}}</a>
</li>
{{/images}}
</ul>
"
}
fn load_images() -> Vec<Image> {
let mut images = Vec::new();
images.push(Image::new("Picture 1", "picture-1.png"));
images.push(Image::new("Picture 2", "picture-2.png"));
images.push(Image::new("Picture 3", "picture-3.png"));
images
}
with_struct.rs:
use std::io;
use mustache;
#[deriving(Encodable)]
struct Image {
name: String,
file: String
}
#[deriving(Encodable)]
struct TemplateData<'a> {
images: &'a Vec<Image>
}
impl Image {
fn new(name: &str, file: &str) -> Image {
Image {
name: String::from_str(name),
file: String::from_str(file)
}
}
}
pub fn go() {
let images = load_images();
let template = mustache::compile_str(template());
let data = TemplateData {
images: &images
};
let _ = template.render(&mut io::stdout(), &data);
}
fn template<'a>() -> &'a str {
"
<ul>
{{#images}}
<li>
<a href=\"{{file}}\">{{name}}</a>
</li>
{{/images}}
</ul>
"
}
fn load_images() -> Vec<Image> {
let mut images = Vec::new();
images.push(Image::new("Picture 1", "picture-1.png"));
images.push(Image::new("Picture 2", "picture-2.png"));
images.push(Image::new("Picture 3", "picture-3.png"));
images
}