Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2bec7f8

Browse files
committed
added config_tag plugin for integration of configuration into templates
1 parent 1c79199 commit 2bec7f8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

plugins/config_tag.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'json'
2+
3+
class ConfigTag < Liquid::Tag
4+
def initialize(tag_name, options, tokens)
5+
super
6+
@options = options.split(' ').map {|i| i.strip }
7+
@key = @options.first
8+
@tag = (@options[1] || 'div')
9+
end
10+
11+
def render(context)
12+
config = context.registers[:site].config
13+
options = @options.first.split('.').map { |k| config = config[k] }.last #reference objects with dot notation
14+
keyclass = @key.sub(/_/, '-').sub(/\./, '-')
15+
tag = "<#{@tag} class='#{keyclass}'"
16+
options.each do |k,v|
17+
unless v.nil?
18+
v = v.join ',' if v.respond_to? 'join'
19+
v = v.to_json if v.respond_to? 'keys'
20+
tag += " data-#{k.sub'_','-'}='#{v}'"
21+
end
22+
end
23+
tag += "></#{@tag}>"
24+
p tag
25+
tag
26+
end
27+
end
28+
29+
Liquid::Template.register_tag('config_tag', ConfigTag)

0 commit comments

Comments
 (0)