Create array in Jekyll page/post using Liquid tag

66 Views Asked by At

I've created a plugin for my Jekyll site 1. The plugin creates a new Liquid tag {% facility name %}. Whenever I use that tag, I want to create/add to an array on that page/post called facilities. Thus, I have a list of facilities mentioned on that page2.

Unfortunately, what I've done below, doesn't work. When I try to access the page.facilities array with {{ page.facilities | jsonify }} I get: null. I know the array gets populated as evidenced by the commented-out print statement; it seems that the value of the facilities array gets lost sometime later.

Please advise.


module Jekyll
  class Facility < Liquid::Tag
    def initialize(tag_name, input, tokens)
      super
      @name = input.strip
    end

    def render(context)
      begin
        page = context.environments.first['page']
        facility = context.registers[:site].data['facilities']["#{@name}"]

        @tags = page['tags']
        @tags.append(@name) unless @tags.include?(@name)

        page['facilities'] = [] unless page.key?('facilities')
        @facilities = page['facilities']
        # print( "Facilities: #{@facilities}")
        @facilities << @name  unless @facilities.include?(@name)

        definition = facility['name']
        url = facility['url']
        if url.nil?
          "<span title=\"#{definition}\" class=\"facility\">#{@name}</span>"
        else
          "<a href=\"#{url}\" title=\"#{definition}\" class=\"organization\" target=\"_blank\">#{@name}</a>"
        end

      rescue NoMethodError
        "<span style=\"color:red\" class=\"facility\">#{@name}</span>"
      end
    end
  end
end

Liquid::Template.register_tag('facility', Jekyll::Facility)

1 Sorry the site is not publicly viewable.

2 The natural follow-on would be to create a page that lists all the posts where a facility is mentioned. This should be a minor extrapolation from tags.

0

There are 0 best solutions below