Sha256: e6db5a8a66447ace912a43cc2d73b9ea3e72af6b2bdebfb0ed288bb11c5281e6

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 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]

      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.new(path).render(scope, locals, &block)
            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

9 entries across 9 versions & 1 rubygems

Version Path
stasis-0.1.8 lib/stasis/plugins/render.rb
stasis-0.1.7 lib/stasis/plugins/render.rb
stasis-0.1.6 lib/stasis/plugins/render.rb
stasis-0.1.5 lib/stasis/plugins/render.rb
stasis-0.1.4 lib/stasis/plugins/render.rb
stasis-0.1.3 lib/stasis/plugins/render.rb
stasis-0.1.2 lib/stasis/plugins/render.rb
stasis-0.1.1 lib/stasis/plugins/render.rb
stasis-0.1.0 lib/stasis/plugins/render.rb