Sha256: b3f4831db6bc6bed7a8f0fd9f39d5683559ea00d6a4672d3be558ab98a7ebeed

Contents?: true

Size: 1.82 KB

Versions: 10

Compression:

Stored size: 1.82 KB

Contents

require 'tilt'

class Stasis
  class Render < Plugin

    action_method :render

    def initialize(stasis)
      @stasis = stasis
    end

    # This method is bound to all actions.
    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]
      scope = options[:scope]
      text = options[:text]
      template_options = 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?(File.extname(path)[1..-1])
              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

    private

    # Temporarily set `Stasis#path`.
    def temporary_path(path, &block)
      @stasis.path, old_path = path, @stasis.path
      yield
      @stasis.path = old_path
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
stasis-0.2.0.pre lib/stasis/plugins/render.rb
stasis-0.1.23 lib/stasis/plugins/render.rb
stasis-0.1.22 lib/stasis/plugins/render.rb
stasis-0.1.21 lib/stasis/plugins/render.rb
stasis-0.1.20 lib/stasis/plugins/render.rb
stasis-0.1.19 lib/stasis/plugins/render.rb
stasis-0.1.18 lib/stasis/plugins/render.rb
stasis-0.1.17 lib/stasis/plugins/render.rb
stasis-0.1.16 lib/stasis/plugins/render.rb
stasis-0.1.15 lib/stasis/plugins/render.rb