class Liquid::Environment

The Environment is the container for all configuration options of Liquid, such as the registered tags, filters, and the default error mode.

Attributes

default_resource_limits[RW]

The default resource limits that are used to limit the resources that a template can consume.

error_mode[RW]

The default error mode for all templates. This can be overridden on a per-template basis.

exception_renderer[RW]

The exception renderer that is used to render exceptions that are raised when rendering a template

file_system[RW]

The default file system that is used to load templates from.

strainer_template[RW]

The strainer template which is used to store filters that are available to use in templates.

tags[RW]

The tags that are available to use in the template.

Public Class Methods

build(tags: nil, file_system: nil, error_mode: nil, exception_renderer: nil) { |ret| ... } click to toggle source

Creates a new environment instance.

@param tags [Hash] The tags that are available to use in

the template.

@param file_system The default file system that is used

to load templates from.

@param error_mode [Symbol] The default error mode for all templates

(either :strict, :warn, or :lax).

@param exception_renderer [Proc] The exception renderer that is used to

render exceptions.

@yieldparam environment [Environment] The environment instance that is being built. @return [Environment] The new environment instance.

# File lib/liquid/environment.rb, line 42
def build(tags: nil, file_system: nil, error_mode: nil, exception_renderer: nil)
  ret = new
  ret.tags = tags if tags
  ret.file_system = file_system if file_system
  ret.error_mode = error_mode if error_mode
  ret.exception_renderer = exception_renderer if exception_renderer
  yield ret if block_given?
  ret.freeze
end
dangerously_override(environment) { || ... } click to toggle source

Sets the default environment instance for the duration of the block

@param environment [Environment] The environment instance to use as the default for the

duration of the block.

@yield @return [Object] The return value of the block.

# File lib/liquid/environment.rb, line 65
def dangerously_override(environment)
  original_default = @default
  @default = environment
  yield
ensure
  @default = original_default
end
default() click to toggle source

Returns the default environment instance.

@return [Environment] The default environment instance.

# File lib/liquid/environment.rb, line 55
def default
  @default ||= new
end
new() click to toggle source

Initializes a new environment instance. @api private

# File lib/liquid/environment.rb, line 76
def initialize
  @tags = Tags::STANDARD_TAGS.dup
  @error_mode = :lax
  @strainer_template = Class.new(StrainerTemplate).tap do |klass|
    klass.add_filter(StandardFilters)
  end
  @exception_renderer = ->(exception) { exception }
  @file_system = BlankFileSystem.new
  @default_resource_limits = Const::EMPTY_HASH
  @strainer_template_class_cache = {}
end

Public Instance Methods

create_strainer(context, filters = Const::EMPTY_ARRAY) click to toggle source

Creates a new strainer instance with the given filters, caching the result for faster lookup.

@param context [Liquid::Context] The context that the strainer will be

used in.

@param filters [Array<Module>] The filters that the strainer will have

access to.

@return [Liquid::Strainer] The new strainer instance.

# File lib/liquid/environment.rb, line 124
def create_strainer(context, filters = Const::EMPTY_ARRAY)
  return @strainer_template.new(context) if filters.empty?

  strainer_template = @strainer_template_class_cache[filters] ||= begin
    klass = Class.new(@strainer_template)
    filters.each { |f| klass.add_filter(f) }
    klass
  end

  strainer_template.new(context)
end
filter_method_names() click to toggle source

Returns the names of all the filter methods that are available to use in the strainer template.

@return [Array<String>] The names of all the filter methods.

# File lib/liquid/environment.rb, line 140
def filter_method_names
  @strainer_template.filter_method_names
end
freeze() click to toggle source
Calls superclass method
# File lib/liquid/environment.rb, line 152
def freeze
  @tags.freeze
  # TODO: freeze the tags, currently this is not possible because of liquid-c
  # @strainer_template.freeze
  super
end
register_filter(filter) click to toggle source

Registers a new filter with the environment.

@param filter [Module] The module that contains the filter methods. @return [void]

# File lib/liquid/environment.rb, line 101
def register_filter(filter)
  @strainer_template_class_cache.clear
  @strainer_template.add_filter(filter)
end
register_filters(filters) click to toggle source

Registers multiple filters with this environment.

@param filters [Array<Module>] The modules that contain the filter methods. @return [self]

# File lib/liquid/environment.rb, line 110
def register_filters(filters)
  @strainer_template_class_cache.clear
  filters.each { |f| @strainer_template.add_filter(f) }
  self
end
register_tag(name, klass) click to toggle source

Registers a new tag with the environment.

@param name [String] The name of the tag. @param klass [Liquid::Tag] The class that implements the tag. @return [void]

# File lib/liquid/environment.rb, line 93
def register_tag(name, klass)
  @tags[name] = klass
end
tag_for_name(name) click to toggle source

Returns the tag class for the given tag name.

@param name [String] The name of the tag. @return [Liquid::Tag] The tag class.

# File lib/liquid/environment.rb, line 148
def tag_for_name(name)
  @tags[name]
end