How to get categories of a post for an Octopress plugin in class Liquid::Tag -
i have code:
module jekyll class connexetag < liquid::tag def render(context) categories = get_categories(context) categories.class.name # => "array" # categories # => "category1category2" # categories.join(',') # => error ! # categories.size # => error ! end private def get_categories(context) context.environments.first["page"]["categories"] end end end
it outputs array, , that's ok. when try methods on categories
, size
or each
error:
building site: source -> public liquid exception: undefined method `size' nil:nilclass in atom.xml /home/xavier/octopress/plugins/connexe_tag.rb:25:in `render'
i can't apply methods on categories
. tell me doing wrong here ?
happily, fix simple. problem code assumes every page have array of categories. isn't case atom.xml context.environments.first["page"]["categories"]
return nil
which, of course, doesn't have method 'size'. can set output if get_categories returns value , you're set.
module jekyll class connexetag < liquid::tag def render(context) categories = get_categories(context) # return list of categories pages have them categories.join(', ') if categories end private def get_categories(context) context.environments.first["page"]["categories"] end end end liquid::template.register_tag('connexe_tag', jekyll::connexetag)
that should it.
Comments
Post a Comment