1

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
}
1

Ollie!

You need to remove the semicolon after last insert_str (Rust discards that line, and don't use as return value, if you put semicolon in the end).

5
  • Hi, thanks. I think I was there too, the problem is that the builder becomes moved in the loop. I tried hacking around with let mut b = &mut builder; but that didn't work and builder isn't clonable. – Ollie Sep 23 '14 at 13:29
  • Then, you just need mutable variable for builder, and do "builder = builder.push_map(..)" – Paul Colomiets Oct 1 '14 at 20:28
  • Yeah, that's probably the problem as I can't do this ".insert_vec("images", |builder| { let mut b = &mut builder.clone(); ... }". – Ollie Oct 6 '14 at 9:21
  • You don't need clone. Just |mut builder| { for image in images.iter() { builder = builder.push_map(...); } return builder } or |bld| { let mut builder = bld; for image in images.iter() { builder = builder.push_map(...); } return builder } – Paul Colomiets Oct 7 '14 at 6:52
  • Ah, I tried that too but was missing the builder = builder reassignment. Thanks, it works now. :-) Funny how brain can forget the basic when encountering the complicated. – Ollie Oct 7 '14 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.