class Liquid::Assign

@liquid_public_docs @liquid_type tag @liquid_category variable @liquid_name assign @liquid_summary

Creates a new variable.

@liquid_description

You can create variables of any [basic type](/api/liquid/basics#types), [object](/api/liquid/objects), or object property.

@liquid_syntax

{% assign variable_name = value %}

@liquid_syntax_keyword variable_name The name of the variable being created. @liquid_syntax_keyword value The value you want to assign to the variable.

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