class Liquid::Assign

Assign sets a variable in your template.

{% assign foo = 'monkey' %}

You can then use the variable later in the page.

{{ foo }}

Constants

Syntax

Attributes

from[R]
to[R]

Public Class Methods

new(tag_name, markup, parse_context) click to toggle source
Calls superclass method Liquid::Tag::new
# 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
raise_syntax_error(parse_context) click to toggle source

@api private

# 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

Public Instance Methods

blank?() click to toggle source
# File lib/liquid/tags/assign.rb, line 39
def blank?
  true
end
render_to_output_buffer(context, output) click to toggle source
# 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

Private Instance Methods

assign_score_of(val) click to toggle source
# File lib/liquid/tags/assign.rb, line 45
def assign_score_of(val)
  if val.instance_of?(String)
    val.bytesize
  elsif val.instance_of?(Array)
    sum = 1
    # Uses #each to avoid extra allocations.
    val.each { |child| sum += assign_score_of(child) }
    sum
  elsif val.instance_of?(Hash)
    sum = 1
    val.each do |key, entry_value|
      sum += assign_score_of(key)
      sum += assign_score_of(entry_value)
    end
    sum
  else
    1
  end
end