Sha256: 72ee071635d13b05448debd29494c02d9bcfc062e38cb9a982916299382d0c3f

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Archangel
  ##
  # Liquid render service
  #
  class RenderService
    ##
    # Variable assignments
    #
    attr_reader :assigns

    ##
    # Local filters
    #
    attr_reader :local_filters

    ##
    # Local registers
    #
    attr_reader :local_registers

    ##
    # Template to Liquidize
    #
    attr_reader :template

    ##
    # Liquid renderer
    #
    # @param template [String] the Liquid template
    # @param assigns [Object] the variable assignments
    # @param options [Object] the Liquid options
    #
    def initialize(template, assigns = {}, options = {})
      @template = template
      @assigns = assigns
      @local_filters = options.fetch(:filters, [])
      @local_registers = options.fetch(:registers, {})
    end

    ##
    # Render the Liquid content
    #
    # @param template [String] the content
    # @param assigns [Hash] the local variables
    # @param options [Hash] the options
    # @return [String] the rendered content
    #
    def self.call(template, assigns = {}, options = {})
      new(template, assigns, options).call
    end

    ##
    # Render the Liquid content
    #
    # @return [String] the rendered content
    #
    def call
      liquid = ::Liquid::Template.parse(template)
      liquid.send(:render, stringify_assigns, liquid_options).html_safe
    end

    protected

    def stringify_assigns
      assigns.deep_stringify_keys
    end

    def liquid_options
      {
        filters: local_filters,
        registers: local_registers,
        error_mode: error_mode,
        strict_variables: strict_variables,
        strict_filters: strict_filters
      }
    end

    def error_mode
      :lax
    end

    def strict_variables
      false
    end

    def strict_filters
      false
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
archangel-0.3.0 app/services/archangel/render_service.rb
archangel-0.0.8 app/services/archangel/render_service.rb
archangel-0.0.7 app/services/archangel/render_service.rb
archangel-0.0.6 app/services/archangel/render_service.rb