Class: Liquid::CaptureVariable

Inherits:
Block
  • Object
show all
Defined in:
app/liquid/blocks/capture_variable.rb

Overview

Capture stores the result of a block into a variable without rendering it inplace.

{% capture_variable heading %}
  {% my_tag param1:value1 param2:value2 %}
{% endcapture_variable %}
...
<h1>{{ heading }}</h1>

Capture is useful for saving content for use later in your template, such as in a sidebar or footer.

Constant Summary collapse

Syntax =
/(\w+)/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ CaptureVariable

Returns a new instance of CaptureVariable.



17
18
19
20
21
22
23
24
25
# File 'app/liquid/blocks/capture_variable.rb', line 17

def initialize(tag_name, markup, tokens)
  if markup =~ Syntax
    @to = $1
  else
    raise SyntaxError.new("Syntax Error in 'capture_variable' - Valid syntax: capture_variable [var]")
  end

  super
end

Instance Method Details

#render(context) ⇒ Object



27
28
29
30
31
32
# File 'app/liquid/blocks/capture_variable.rb', line 27

def render(context)
  context.scopes.last['capture_variable'] = @to
  render_all(@nodelist, context)
  context.scopes.last.except!('capture_variable')
  ''
end