user1413969
user1413969

Reputation: 1291

Underscore's template method with multiple arguments

On the Underscore.js site, we're given the code example:

var list = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>";
_.template(list, {people: ['moe', 'curly', 'larry']});

=> "<li>moe</li><li>curly</li><li>larry</li>"

Lets assume that I want to pass 2 array of values instead of 1 (@ people). So that I could do something like:

{ %> <li><%= name %> , <%= address %></li> <% }

I tinkered around for a bit and wasn't sure how to use the method for this.

Upvotes: 3

Views: 1717

Answers (1)

McGarnagle
McGarnagle

Reputation: 102753

I believe you'd have to make the array into an array of objects with "name" and "address" properties:

_.template(list, {people: [ 
    { name: 'moe', address: 'foo'}, 
    { name: 'curly', address: 'bar' } 
] });

Then the parameter would be the object "person" instead of just the string "name":

var list = "<% _.each(people, function(person) { %> <li>Name: <%= person.name %>, Address: <%= person.address %></li> <% }); %>";

Upvotes: 4

Related Questions