librelist archives

« back to archive

Mustache improvement suggestion: need a way to set markup *between* list items, not around

Mustache improvement suggestion: need a way to set markup *between* list items, not around

From:
Emmett Pickerel
Date:
2011-03-23 @ 22:44
I have arrays of items that need to be *separated* by markup rather than
*surrounded* by it. Examples are CSV, semicolon separated, pipe separated,
and so forth. I can't find a way to do this.

Example:
{
    names: [ "bob", "joe", "walter" ]
}
...should become:
bob, joe, walter

Unfortunately, with Mustache (which I otherwise love), it seems there's no
way to accomplish it. The array needs to be set to a string in the
controller, which means that the delimiting markup must be also defined in
the controller instead of the view!

I don't think this qualifies as dreaded template logic any more than list
loops themselves, since it simply embodies the idea of "between".

Re: [mustache] Mustache improvement suggestion: need a way to set markup *between* list items, not around

From:
Pieter V.
Date:
2011-03-24 @ 02:19
There are a number of solutions that "almost work", but too few that
actually do, I agree.

The simplest solution I can come up with off the top of my head (that
*actually* works) is this:

    Mustache.render '{{list}}', { :list => proc { list.join(', ') } }
    => "bob, joe, walter"

Obviously enough, that's not actually a solution, and for some cases, simply
pushing that logic down into your View class doesn't fix it either.

New syntaxes are easy to contrive (e.g. `{{# list [separator] }}`,
`{{#list}}{{.}}{{*sep}}{{/list}}`), but I feel that if the pattern of "write
/ inject this for all but the final iteration" can be addressed -- simply --
by the current syntax, we need to not only acknowledge it but document it
well.  This is a fairly common problem, and should have a simple solution.