Sha256: 062a75cd94258d9b3a45503b4afb8e427e3ac8e4743366c4c343a83620f1afd7

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

require 'orange/core'
require 'orange/middleware/base'

module Orange::Middleware
  
  class Template < Base
    def init(*args)
      @core.add_pulp(Orange::Pulp::Template)
      @core.mixin(Orange::Mixins::Template)
      
      # Establish a default template chooser
      @core.template_chooser do |packet|
        if packet['route.context'] == :admin
          packet.add_css('admin.css', :module => '_orange_')
          packet.add_js('jquery.js', :module => '_orange_')
          packet.add_js('admin.js', :module => '_orange_')
          'admin.haml'
        else
          false
        end
      end
    end
    
    def packet_call(packet)
      packet['template.file'] = orange.template_for packet
      status, headers, content = pass packet
      if needs_wrapped?(packet)
        content = wrap(packet, content)
        packet[:content] = content.first
        orange.fire(:wrapped, packet)
      end  
      orange.fire(:after_wrap, packet)
      packet.finish
    end
    
    def needs_wrapped?(packet)
      packet['template.file'] && !packet['template.disable']
    end
    
    def wrap(packet, content = false)
      content = packet.content unless content
      content = content.join
      content = orange[:parser].haml(packet['template.file'], packet, :wrapped_content => content, :template => true) do 
        content
      end
      [content]
    end
  end
end

module Orange::Pulp::Template
  def wrap
    packet[:content] = orange[:parser].haml(packet['template.file'], packet, :wrapped_content => packet[:content], :template => true) do
      content
    end
  end
end

module Orange::Mixins::Template
  def template_for(packet)
    @template_chooser.call(packet)
  end
  def template_chooser(&block)
    @template_chooser = Proc.new
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
orange-0.0.13 lib/orange/middleware/template.rb
orange-0.0.12 lib/orange/middleware/template.rb
orange-0.0.10 lib/orange/middleware/template.rb
orange-0.0.8 lib/orange/middleware/template.rb
orange-0.0.7 lib/orange/middleware/template.rb
orange-0.0.6 lib/orange/middleware/template.rb
orange-0.0.5 lib/orange/middleware/template.rb