Class: Liquid::Assign

Inherits:
Tag
  • Object
show all
Defined in:
lib/liquid/tags/assign.rb

Overview

Assign sets a variable in your template.

{% assign foo = 'monkey' %}

You can then use the variable later in the page.

{{ foo }}

Defined Under Namespace

Classes: ParseTreeVisitor

Constant Summary collapse

Syntax =
/(#{VariableSignature}+)\s*=\s*(.*)\s*/om

Instance Attribute Summary collapse

Attributes inherited from Tag

#line_number, #nodelist, #parse_context, #tag_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tag

disable_tags, #name, parse, #parse, #raw, #render

Methods included from ParserSwitching

#parse_with_selected_parser, #strict_parse_with_error_mode_fallback

Constructor Details

#initialize(tag_name, markup, parse_context) ⇒ Assign

Returns a new instance of Assign.



22
23
24
25
26
27
28
29
30
# File 'lib/liquid/tags/assign.rb', line 22

def initialize(tag_name, markup, parse_context)
  super
  if markup =~ Syntax
    @to   = Regexp.last_match(1)
    @from = Variable.new(Regexp.last_match(2), parse_context)
  else
    self.class.raise_syntax_error(parse_context)
  end
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



20
21
22
# File 'lib/liquid/tags/assign.rb', line 20

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



20
21
22
# File 'lib/liquid/tags/assign.rb', line 20

def to
  @to
end

Class Method Details

.raise_syntax_error(parse_context) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/liquid/tags/assign.rb', line 16

def self.raise_syntax_error(parse_context)
  raise Liquid::SyntaxError, parse_context.locale.t('errors.syntax.assign')
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/liquid/tags/assign.rb', line 39

def blank?
  true
end

#render_to_output_buffer(context, output) ⇒ Object



32
33
34
35
36
37
# File 'lib/liquid/tags/assign.rb', line 32

def render_to_output_buffer(context, output)
  val = @from.render(context)
  context.scopes.last[@to] = val
  context.resource_limits.increment_assign_score(assign_score_of(val))
  output
end