Class: Stasis::Render

Inherits:
Plugin
  • Object
show all
Defined in:
lib/stasis/plugins/render.rb

Instance Method Summary collapse

Methods inherited from Plugin

#_match_key?, _priority, #_within?, inherited, plugins, priority

Constructor Details

#initialize(stasis) ⇒ Render

Returns a new instance of Render.



8
9
10
# File 'lib/stasis/plugins/render.rb', line 8

def initialize(stasis)
  @stasis = stasis
end

Instance Method Details

#render(path_or_options = {}, options = {}, &block) ⇒ Object

This method is bound to all actions.



13
14
15
16
17
18
19
20
21
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/stasis/plugins/render.rb', line 13

def render(path_or_options={}, options={}, &block)
  if path_or_options.is_a?(::String)
    options[:path] = path_or_options
  else
    options.merge!(path_or_options)
  end

  callback = options[:callback]
  locals = options[:locals]
  path = options[:path]
  ext = File.extname(path.to_s)[1..-1]
  scope = options[:scope]
  text = options[:text]
  template_options = Options.get_template_option(ext).merge( options[:template] || {} )

  if @stasis.controller
    path = @stasis.controller._resolve(path)
  end
  
  output =
    if text
      text
    elsif path && File.file?(path)
      unless callback == false
        # Trigger all plugin `before_render` events.
        temporary_path(path) do
          @stasis.trigger(:before_render)
        end
      end

      output =
        if Tilt.mappings.keys.include?(ext)
          scope = options[:scope] ||= @stasis.action
          tilt = Tilt.new(path, nil, template_options)
          if block_given?
            tilt.render(scope, locals, &block)
          else
            tilt.render(scope, locals) { nil }
          end
        else
          File.read(path)
        end

      unless callback == false
        # Trigger all plugin `after_render` events.
        temporary_path(path) do
          @stasis.trigger(:after_render)
        end
      end

      output
    end
  
  output
end