Class: Liquid::Capture

Inherits:
Block show all
Defined in:
lib/liquid/tags/capture.rb

Overview

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

{% capture heading %}
  Monkeys!
{% endcapture %}
...
<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+)/

Constants inherited from Block

Block::ContentOfVariable, Block::FullToken, Block::IsTag, Block::IsVariable

Instance Attribute Summary

Attributes inherited from Tag

#line, #nodelist, #options, #warnings

Instance Method Summary collapse

Methods inherited from Block

#block_delimiter, #block_name, #create_variable, #end_tag, #parse, #unknown_tag, #warnings

Methods inherited from Tag

#name, new_with_options, #parse, #parse_with_selected_parser

Constructor Details

#initialize(tag_name, markup, tokens, options) ⇒ Capture

Returns a new instance of Capture.



17
18
19
20
21
22
23
24
25
# File 'lib/liquid/tags/capture.rb', line 17

def initialize(tag_name, markup, tokens, options)
  if markup =~ Syntax
    @to = $1
  else
    raise SyntaxError.new(options[:locale].t("errors.syntax.capture"), options[:line])
  end

  super
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/liquid/tags/capture.rb', line 34

def blank?
  true
end

#render(context) ⇒ Object



27
28
29
30
31
32
# File 'lib/liquid/tags/capture.rb', line 27

def render(context)
  output = super
  context.scopes.last[@to] = output
  context.resource_limits[:assign_score_current] += (output.respond_to?(:length) ? output.length : 1)
  ''
end