content between each element of a list

1,527 views
Skip to first unread message

Alexandre Morgaut

unread,
Nov 17, 2010, 4:59:36 AM11/17/10
to Mustache
Hi,

Mustache allows me to generate content from a list

The main seen example looks like this:

{{#list}}
<li>{{.}}</li>
{{/list}}

or like this

{{#employees}}
<li>{{firstName}} {{name}}</li>
{{/employees}}

which is great
but I still have a problem actually

Lets imagine I want to represent the values of the list in a comma
separated way:

My favorite topics: {{#topics}} {{.}}, {{/topics}}

The comma will also be inserted after the last element

Does Mustache provide a way to resolve this ?


Alexandre Morgaut

unread,
Nov 17, 2010, 12:18:31 PM11/17/10
to Mustache
As we have
{{%IMPLICIT-ITERATOR}}

Have we (or can we have)

{{%IMPLICIT-SEPARATOR separator=", "}}

for which the default value would be the empty string ?

Alexandre Morgaut

unread,
Nov 18, 2010, 3:46:32 AM11/18/10
to Mustache
I'm wondering if {{%IMPLICIT-ITERATOR}} is standard in Mustache
implementations

I saw it in the JavaScript version (https://github.com/janl/
mustache.js)

But I didn't saw it in the ruby implementation (maybe I missed it)

By the way, it can be an interesting option to make templates more
readable by designers, while defining a "SEPARATOR" would resolve some
important problems.

Chris Wanstrath

unread,
Nov 18, 2010, 9:44:59 PM11/18/10
to musta...@googlegroups.com
On Wed, Nov 17, 2010 at 1:59 AM, Alexandre Morgaut
<alexandr...@4d.com> wrote:

> My favorite topics: {{#topics}} {{.}}, {{/topics}}
>
> The comma will also be inserted after the last element
>
> Does Mustache provide a way to resolve this ?

It's not built in.

Google's CTemplate (which Mustache is based on) supports this:

Here are the meeting attendees:
{{#ATTENDEES}}
{{NAME}}
{{#ATTENDEES_separator}}, {{/ATTENDEES_separator}}
{{/ATTENDEES}}
.

But in Mustache I've just been adding that logic to the View:

My favorite topics: {{#topics}} {{.}}{{^last_topic?}},{{/last_topic?}}
{{/topics}}

This might be more difficult in mustache.js though.

Alexandre Morgaut

unread,
Dec 7, 2010, 2:40:03 PM12/7/10
to Mustache

> But in Mustache I've just been adding that logic to the View:
>
> My favorite topics: {{#topics}} {{.}}{{^last_topic?}}, {{/last_topic?}}{{/topics}}
>
> This might be more difficult in mustache.js though.

So true...
I planned to use this kind of "trick" but {{^tagname}} is not
supported yet in the JS version

This will work better :-(
> My favorite topics: {{#topics}} {{.}}{{#not_last_topic}}, {{/not_last_topic}}{{/topics}}

Reply all
Reply to author
Forward
0 new messages