liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Is it possible to store a variable like in Capture but make it available everywhere in the parent scope?

Open gitleet opened this issue 7 years ago • 2 comments

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?

gitleet avatar Nov 10 '19 22:11 gitleet

Any help?

gitleet avatar Nov 20 '19 02:11 gitleet

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.

ZacxDev avatar Dec 02 '19 03:12 ZacxDev