liquid
liquid copied to clipboard
Is it possible to store a variable like in Capture but make it available everywhere in the parent scope?
How can I create a Block tag that behaves like a Capture tag, but I want it to be available anywhere in the calling template, not ONLY after it.
For example, the Capture tag looks like:
class Capture < Block
Syntax = /(#{VariableSignature}+)/o
def initialize(tag_name, markup, options)
super
if markup =~ Syntax
@to = $1
else
raise SyntaxError.new(options[:locale].t("errors.syntax.capture"))
end
end
def render(context)
output = super
context.scopes.last[@to] = output
context.resource_limits.assign_score += output.length
''.freeze
end
def blank?
true
end
end
And it currently works like:
{% capture heading %}
Monkeys!
{% endcapture %}
<h1>{{ heading }}</h1>
If you put {{heading}} before the definition of the variable, it doesn't work. Is it possible to make it work even BEFORE the definition? Somehow put the variable in the parent scope?
Any help?
The technique you're talking about is called "variable hoisting", a few popular languages have the feature, a prime example being Javascript. Unfortunately, Liquid does not support this technique.