File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments