Custom Jekyll filter for tags

This is a simple Jekyll filter that I wrote in order to use Bootstrap’s label objects to display post tags.
It’s based on the original array\_to\_sentence\_string filter, part of the Jekyll core.

Just put it in your _plugins folder and use it in your layout:

module Jekyll
  module CustomTagFilter
    def tag_array(array)
      connector = ","
      case array.length
        when 0
          ""
        when 1
          "<span class=\"label label-default\">#{ array[0].to_s}</span>"
        else
          "<span class=\"label label-default\">#{array.join('</span> <span class="label label-default">')}</span>"
      end
    end
  end
end

Liquid::Template.register_filter(Jekyll::CustomTagFilter)

Usage

<span class="posttags">{ { page.tags | tag_array } } </span>

Result

Tags: TechJekyllBootstrap