|
3 | 3 | class ConfigTag < Liquid::Tag
|
4 | 4 | def initialize(tag_name, options, tokens)
|
5 | 5 | 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 |
9 | 14 | end
|
10 | 15 |
|
11 | 16 | 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' |
16 | 28 | options.each do |k,v|
|
17 | 29 | unless v.nil?
|
18 | 30 | v = v.join ',' if v.respond_to? 'join'
|
19 | 31 | v = v.to_json if v.respond_to? 'keys'
|
20 |
| - tag += " data-#{k.sub'_','-'}='#{v}'" |
| 32 | + output += " data-#{k.sub'_','-'}='#{v}'" |
21 | 33 | end
|
22 | 34 | 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]}'" |
26 | 39 | end
|
| 40 | + output += "></#{tag}>" |
27 | 41 | end
|
28 | 42 |
|
29 | 43 | Liquid::Template.register_tag('config_tag', ConfigTag)
|
| 44 | + |
0 commit comments