Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any equivalence to Dust's @sep-arator concept? #50

Open
mjudd opened this issue Oct 18, 2012 · 1 comment
Open

Any equivalence to Dust's @sep-arator concept? #50

mjudd opened this issue Oct 18, 2012 · 1 comment

Comments

@mjudd
Copy link

mjudd commented Oct 18, 2012

Dust.js (at least LinkedIn's version) has a useful {@sep} capability to denote what "separator" should be used when generating list entries. It's akin to the "join" operator for an array.

Dust:  {#links}<a href='/nodes/{href}'>{name}</a>{@sep}, {/sep}{/links}

I can't quite figure out the best way to do this in coffeecup. It's easy to generate a list of links:

a href:link.href, link.name for link in links

but producing them as a comma-separated list is more problematic.

Since coffeecup is "generating" results to a special buffer rather than "returning" results to coffeescript, I can't use a normal "join" operator.

I'm hoping there's a nifty mechanism to do this without resorting to index counting and emitting a text ', ' for all-but-the-last list item.

Any solutions to this? Can I encapsulate this sort of functionality in a helper somehow? Am I overlooking something trivial?

@mjudd
Copy link
Author

mjudd commented Oct 20, 2012

Well, it was easy enough to write a helper function

forEachWithSep: (list, itemFn, sep) -> 
  for idx, item of list
    itemFn item
    if idx < list.length - 1
      switch typeof sep
        when 'string'
          text sep
         when 'function'
           sep()

that I could then call generically

forEachWithSep links,
  (link) -> a href:"/#{link.path}", link.name ## Note: arbitrary item handler function
  ", "

Any other tactics come to mind?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant