Class: Liquid::Autoescape::TemplateVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/autoescape/template_variable.rb

Overview

A wrapper around a Liquid variable used in a template

This provides a consistent interface to a Liquid variable, accounting for structural differences in variables between different Liquid versions and exposing a simple list of applied filters. All exemptions are determined by examining instances of this object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TemplateVariable

Create a wrapper around a Liquid variable used in a template

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :name (String)

    The name of the variable

  • :filters (Array<Symbol>)

    The filters applied to the variable



66
67
68
69
# File 'lib/liquid/autoescape/template_variable.rb', line 66

def initialize(options={})
  @name = options.fetch(:name)
  @filters = options[:filters] || []
end

Instance Attribute Details

#filtersArray<Symbol> (readonly)

The names of the filters applied to the variable

Returns:

  • (Array<Symbol>)


20
21
22
# File 'lib/liquid/autoescape/template_variable.rb', line 20

def filters
  @filters
end

#nameString (readonly)

The name of the variable

Returns:

  • (String)


15
16
17
# File 'lib/liquid/autoescape/template_variable.rb', line 15

def name
  @name
end

Class Method Details

.from_liquid_variable(variable) ⇒ Liquid::Autoescape::TemplateVariable

Create a wrapper around a Liquid variable instance

This normalizes the variable’s information, since Liquid handles variable names differently across versions.

Parameters:

Returns:



31
32
33
34
35
36
# File 'lib/liquid/autoescape/template_variable.rb', line 31

def from_liquid_variable(variable)
  name = normalize_variable_name(variable)
  filters = variable.filters.map { |f| f.first.to_sym }

  new(:name => name, :filters => filters)
end