Class: LiquidView

Inherits:
Object
  • Object
show all
Defined in:
lib/extras/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

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

- (LiquidView) initialize(view)



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

def initialize(view)
  @view = view
end

Class Method Details

+ (Object) call(template)



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

def self.call(template)
  "LiquidView.new(self).render(template, local_assigns)"
end

Instance Method Details

- (Boolean) compilable?



47
48
49
# File 'lib/extras/liquid_view.rb', line 47

def compilable?
  false
end

- (Object) render(template, local_assigns = nil)



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

def render(template, local_assigns = nil)
  @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.controller.master_helper_module], :registers => {:action_view => @view, :controller => @view.controller})
end