Class: Liquid::Autoescape::Exemption

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

Overview

An exemption that may apply to a Liquid template variable

Exemptions are created from functions that accept a template variable and and return a boolean value indicating whether or not the variable is exempt from auto-escaping.

Examples:

An exemption based on a variable’s name

exemption = Exemption.new do |variable|
  variable.name == "safe_variable"
end

An exemption based on a variable’s filters

exemption = Exemption.new do |variable|
  variable.filters.include?(:trusted_filter)
end

Instance Method Summary collapse

Constructor Details

#initialize(&filter) ⇒ Exemption

Create a new auto-escaping exemption

This requires a filter function to be provided that will be passed a TemplateVariable instance that it can use to return a boolean indicating whether the exemption applies to the variable.

Parameters:

  • filter (Proc)

    A filter function to use for calculating the exemption

Raises:



31
32
33
34
# File 'lib/liquid/autoescape/exemption.rb', line 31

def initialize(&filter)
  raise ExemptionError, "You must provide an exemption with a block that determines if an exemption applies" unless block_given?
  @filter = filter
end

Instance Method Details

#applies?(variable) ⇒ Boolean

Determine whether the exemption applies to a Liquid variable

Parameters:

Returns:

  • (Boolean)

    Whether the exemption applies to the variable



40
41
42
# File 'lib/liquid/autoescape/exemption.rb', line 40

def applies?(variable)
  @filter.call(variable)
end