Search code examples
coffeescriptdocpad

How to create a comma separated tag list in Docpad?


The tags for a post can be accessed within the post like this (using embedded Coffeescript):

<div class="tags">
Tags:
  <ul>
    <% for tag in @document.tags: %>
      <li><a class="tag_item" href="<%= @getTagUrl(tag) %>"><%= tag %></a></li>
    <% end %>
  </ul>
</div>

This generates an unordered list of the tags for this specific topic, like this:

Tags:

  • tag1
  • tag2
  • tag3

How can I generate the list of tags as comma separated values on a single line, like this:

Tags: tag1, tag2, tag3


Solution

  • I do it like this on my blog:

    <div class="post-tags">
        Posted In: <%- ("<a href='#{@getTagUrl(tag)}'>#{tag}</a>" for tag in @tags).join(', ') %>
    </div>
    

    Note, @getTagUrl comes from the docpad-plugin-tagging plugin. If you don't want hyperlinks to a page for each tag, you could simplify this to the following:

    <div class="post-tags">
        Posted In: <%- (tag for tag in @tags).join(', ') %>
    </div>