Support passing Liquid variables to includes
Change the regex matching to allow Liquid variables and object fields to be passed to the include. Use the render context to retrieve the variable value. Also, relax syntax checks by allowing surrounding spaces and dashes in parameter names.
This commit is contained in:
parent
f8f6784305
commit
2b01b06ec8
|
@ -2,7 +2,7 @@ module Jekyll
|
||||||
module Tags
|
module Tags
|
||||||
class IncludeTag < Liquid::Tag
|
class IncludeTag < Liquid::Tag
|
||||||
|
|
||||||
MATCHER = /(\w+)=(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)')/
|
MATCHER = /([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))/
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
super
|
super
|
||||||
|
@ -16,7 +16,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_params(markup)
|
def parse_params(markup, context)
|
||||||
params = {}
|
params = {}
|
||||||
pos = 0
|
pos = 0
|
||||||
|
|
||||||
|
@ -42,6 +42,8 @@ eos
|
||||||
value = match[2].gsub(/\\"/, '"')
|
value = match[2].gsub(/\\"/, '"')
|
||||||
elsif match[3]
|
elsif match[3]
|
||||||
value = match[3].gsub(/\\'/, "'")
|
value = match[3].gsub(/\\'/, "'")
|
||||||
|
elsif match[4]
|
||||||
|
value = context[match[4]]
|
||||||
end
|
end
|
||||||
|
|
||||||
params[match[1]] = value
|
params[match[1]] = value
|
||||||
|
@ -67,7 +69,7 @@ eos
|
||||||
partial = Liquid::Template.parse(source)
|
partial = Liquid::Template.parse(source)
|
||||||
|
|
||||||
context.stack do
|
context.stack do
|
||||||
context['include'] = parse_params(@params) if @params
|
context['include'] = parse_params(@params, context) if @params
|
||||||
partial.render(context)
|
partial.render(context)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue