Migrating erb idioms

5 views
Skip to first unread message

Jesús Gabriel y Galán

unread,
Nov 24, 2009, 1:43:04 PM11/24/09
to musta...@googlegroups.com
Hi,

I'm trying out Mustache translating some erb files I have. I use this
idiom quite a bit to dynamically build a select with numbers:

<select name="something"><%= (0..10).map {|x| %Q{<option
value="#{x}">#{x}</option>}} %></select>

What would be the best way to move this to "the Mustache way"? Having
a method in the mustache like

def zero_to_ten
(0..10).map {|i| {:i => i}}
end

and then in the view:

<select name="something">
{{#zero_to_ten}}
<option value="{{i}}">{{i}}</option>
{{/zero_to_ten}}

Doesn't look very nice to me, cause I'd need a method for each range I
would have.
I tried making a method in the view that receives parameters but I
don't know how to call it from the view. I tried:

def range(first,last)
(first..last).map {|i| {:i => i}}
end

{{#range(0,10)}}
<option value="{{i}}">{{i}}</option>
{{/range(0,10)}}

But it doesn't work. I was wondering, in the example about the
gravatar here (http://github.com/defunkt/mustache) how do you go about
calling the methods included in the view such as gravatar(email,
size=30)?

Thanks,

Jesus.

Chris Wanstrath

unread,
Nov 24, 2009, 4:18:20 PM11/24/09
to musta...@googlegroups.com
2009/11/24 Jesús Gabriel y Galán <jgabrie...@gmail.com>:
You can't pass options in this way, no. Mustache wants you to write
all your code in Ruby - the HTML only decides where to display things.
You'd have to write a method for each range.

Of course if you really, really want to be evil: http://gist.github.com/242228

--
Chris Wanstrath
http://github.com/defunkt

Jesús Gabriel y Galán

unread,
Nov 24, 2009, 4:50:22 PM11/24/09
to musta...@googlegroups.com
Thanks, I understand the point. I was a bit confused by the gravatar
example, in which you include a helper module with methods that
receive parameters, which I still don't understand.

> Of course if you really, really want to be evil: http://gist.github.com/242228

Yep, that is tricky, in the end it's masking a way to pass parameters
from the template to the view.

Thanks,

Jesus.
Reply all
Reply to author
Forward
0 new messages