Skip to content

Commit

Permalink
added config_tag plugin for integration of configuration into templates
Browse files Browse the repository at this point in the history
  • Loading branch information
imathis committed Mar 10, 2013
1 parent 1c79199 commit 2bec7f8
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions plugins/config_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'json'

class ConfigTag < Liquid::Tag
def initialize(tag_name, options, tokens)
super
@options = options.split(' ').map {|i| i.strip }
@key = @options.first
@tag = (@options[1] || 'div')
end

def render(context)
config = context.registers[:site].config
options = @options.first.split('.').map { |k| config = config[k] }.last #reference objects with dot notation
keyclass = @key.sub(/_/, '-').sub(/\./, '-')
tag = "<#{@tag} class='#{keyclass}'"
options.each do |k,v|
unless v.nil?
v = v.join ',' if v.respond_to? 'join'
v = v.to_json if v.respond_to? 'keys'
tag += " data-#{k.sub'_','-'}='#{v}'"
end
end
tag += "></#{@tag}>"
p tag
tag
end
end

Liquid::Template.register_tag('config_tag', ConfigTag)

0 comments on commit 2bec7f8

Please sign in to comment.