Sha256: 1d0c64f6dbdf20084816370640eb7fc322076241d4c7194fd991b6d6ebb7c83f

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module Archangel
  ##
  # Liquid view renderer
  #
  class LiquidView
    ##
    # Liquid view
    #
    # @param view [String] the view
    #
    def initialize(view)
      @view = view
    end

    ##
    # Render Liquid content
    #
    # @param template [String] the content
    # @return [String] the rendered content
    #
    def self.call(template)
      "Archangel::LiquidView.new(self).render(
        #{template.source.inspect}, local_assigns)"
    end

    ##
    # Render Liquid content
    #
    # @param template [String] the content
    # @param local_assigns [Hash] the local assigned variables
    # @return [String] the rendered content
    #
    def render(template, local_assigns = {})
      default_controller.headers["Content-Type"] ||= "text/html; charset=utf-8"

      assigns = default_assigns(local_assigns)
      options = { registers: default_registers }

      Archangel::RenderService.call(template, assigns, options)
    end

    protected

    def default_controller
      @view.controller
    end

    def default_assigns(local_assigns)
      assigns = @view.assigns

      if @view.content_for?(:layout)
        assigns["content_for_layout"] = @view.content_for(:layout)
      end

      assigns.merge(local_assigns.stringify_keys)
    end

    def default_registers
      {
        view: @view,
        controller: default_controller,
        helper: ActionController::Base.helpers
      }
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
archangel-0.3.0 lib/archangel/liquid_view.rb
archangel-0.0.8 lib/archangel/liquid_view.rb
archangel-0.0.7 lib/archangel/liquid_view.rb
archangel-0.0.6 lib/archangel/liquid_view.rb
archangel-0.0.5 lib/archangel/liquid_view.rb
archangel-0.0.4 lib/archangel/liquid_view.rb
archangel-0.0.3 lib/archangel/liquid_view.rb