2010-05-30 23:57:33 +00:00
|
|
|
module Locomotive
|
|
|
|
module Liquid
|
|
|
|
module Tags
|
|
|
|
class WithScope < ::Liquid::Block
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def initialize(tag_name, markup, tokens)
|
|
|
|
@attributes = {}
|
|
|
|
markup.scan(::Liquid::TagAttributes) do |key, value|
|
|
|
|
@attributes[key] = value
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|
2010-05-30 23:57:33 +00:00
|
|
|
super
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def render(context)
|
|
|
|
context.stack do
|
|
|
|
context['with_scope'] = decode(@attributes)
|
|
|
|
render_all(@nodelist, context)
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
private
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def decode(attributes)
|
|
|
|
attributes.each_pair do |key, value|
|
|
|
|
attributes[key] = (case value
|
|
|
|
when /true|false/ then value == 'true'
|
|
|
|
when /[0-9]+/ then value.to_i
|
|
|
|
when /'(\S+)'/ then $1
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
::Liquid::Template.register_tag('with_scope', WithScope)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|