Class: LiquidView

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_engine/liquid_view_patch/liquid_view.rb

Overview

LiquidView is a action view extension class. You can register it with rails and use liquid as an template system for .liquid files

Example

ActionView::Base::register_template_handler :liquid, LiquidView

Constant Summary collapse

PROTECTED_ASSIGNS =
%w( template_root response _session template_class action_name request_origin session template
_response url _request _cookies variables_added _flash params _headers request cookies
ignore_missing_templates flash _params logger before_filter_chain_aborted headers )
PROTECTED_INSTANCE_VARIABLES =
%w( @_request @controller @_first_render @_memoized__pick_template @view_paths
@helpers @assigns_added @template @_render_stack @template_format @assigns )

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ LiquidView

Returns a new instance of LiquidView.



18
19
20
# File 'lib/mail_engine/liquid_view_patch/liquid_view.rb', line 18

def initialize(view)
  @view = view
end

Class Method Details

.call(template) ⇒ Object



14
15
16
# File 'lib/mail_engine/liquid_view_patch/liquid_view.rb', line 14

def self.call(template)
  "LiquidView.new(self).render('#{template.source.gsub(/\'/, '"')}', local_assigns)"
end

Instance Method Details

#compilable?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/mail_engine/liquid_view_patch/liquid_view.rb', line 48

def compilable?
  false
end

#render(template, local_assigns = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mail_engine/liquid_view_patch/liquid_view.rb', line 22

def render(template, local_assigns = nil)
  ### when I render text layout, it still set me html content type. so remove it.
  # @view.controller.headers["Content-Type"] ||= 'text/html; charset=utf-8'

  # Rails 2.2 Template has source, but not locals
  if template.respond_to?(:source) && !template.respond_to?(:locals)
    assigns = (@view.instance_variables - PROTECTED_INSTANCE_VARIABLES).inject({}) do |hash, ivar|
                hash[ivar[1..-1]] = @view.instance_variable_get(ivar)
                hash
              end
  else
    assigns = @view.assigns.reject{ |k,v| PROTECTED_ASSIGNS.include?(k) }
  end

  source = template.respond_to?(:source) ? template.source : template
  local_assigns = (template.respond_to?(:locals) ? template.locals : local_assigns) || {}

  if content_for_layout = @view.instance_variable_get("@content_for_layout")
    assigns['content_for_layout'] = content_for_layout
  end
  assigns.merge!(local_assigns.stringify_keys)

  liquid = Liquid::Template.parse(source)
  liquid.render(assigns, :filters => [@view.helpers], :registers => {:action_view => @view, :controller => @view.controller})
end