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

Skip to content

Commit 05db158

Browse files
committed
Improved: config_tag plugin is much more flexible now and can be used by other plugins directly through the config_tag method
1 parent 2bec7f8 commit 05db158

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

plugins/config_tag.rb

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,42 @@
33
class ConfigTag < Liquid::Tag
44
def initialize(tag_name, options, tokens)
55
super
6-
@options = options.split(' ').map {|i| i.strip }
7-
@key = @options.first
8-
@tag = (@options[1] || 'div')
6+
options = options.split(' ').map {|i| i.strip }
7+
@key = options.slice!(0)
8+
@tag = nil
9+
@classname = nil
10+
options.each do |option|
11+
@tag = $1 if option =~ /tag:(\S+)/
12+
@classname = $1 if option =~ /classname:(\S+)/
13+
end
914
end
1015

1116
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}'"
17+
config_tag(context.registers[:site].config, @key, @tag, @classname)
18+
end
19+
end
20+
21+
def config_tag(config, key, tag=nil, classname=nil)
22+
options = key.split('.').map { |k| config[k] }.last #reference objects with dot notation
23+
tag ||= 'div'
24+
classname ||= key.sub(/_/, '-').sub(/\./, '-')
25+
output = "<#{tag} class='#{classname}'"
26+
27+
if options.respond_to? 'keys'
1628
options.each do |k,v|
1729
unless v.nil?
1830
v = v.join ',' if v.respond_to? 'join'
1931
v = v.to_json if v.respond_to? 'keys'
20-
tag += " data-#{k.sub'_','-'}='#{v}'"
32+
output += " data-#{k.sub'_','-'}='#{v}'"
2133
end
2234
end
23-
tag += "></#{@tag}>"
24-
p tag
25-
tag
35+
elsif options.respond_to? 'join'
36+
output += " data-value='#{config[key].join(',')}'"
37+
else
38+
output += " data-value='#{config[key]}'"
2639
end
40+
output += "></#{tag}>"
2741
end
2842

2943
Liquid::Template.register_tag('config_tag', ConfigTag)
44+

0 commit comments

Comments
 (0)