Sha256: f4f97f13ea5aa64c2cca60f003bd9fa4ab8b13355500e9c76664524279a5c850

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

require "haml"

module Capcode
  module Helpers
    @@__HAML_PATH__ = nil
    # Set the path to Haml files. If this path is not set, Capcode will search in the static path.
    def self.haml_path=( p ) #:nodoc:
      @@__HAML_PATH__ = p
    end
    
    def render_haml( f, opts ) #:nodoc:
      if @@__HAML_PATH__.nil?
        @@__HAML_PATH__ = "." + (Capcode.static.nil? == false)?Capcode.static():''
      end
      
      f = f.to_s
      if f.include? '..'
        return [403, {}, '403 - Invalid path']
      end
      
      if /Windows/.match( ENV['OS'] )
        unless( /.:\\/.match( @@__HAML_PATH__[0] ) )
          @@__HAML_PATH__ = File.expand_path( File.join(".", @@__HAML_PATH__) )
        end
      else
        unless( @@__HAML_PATH__[0].chr == "/" )
          @@__HAML_PATH__ = File.expand_path( File.join(".", @@__HAML_PATH__) )
        end
      end
       
      layout = opts.delete(:layout)||:layout
      layout_file = File.join( @@__HAML_PATH__, layout.to_s+".haml" )
      
      f = f + ".haml" if File.extname( f ) != ".haml"
      file = File.join( @@__HAML_PATH__, f )

      if( File.exist?( layout_file ) )
        Haml::Engine.new( open( layout_file ).read ).to_html(self) { |*args| 
          @@__ARGS__ = args
          Haml::Engine.new( open( file ).read ).render(self) 
        }
      else
        Haml::Engine.new( open( file ).read ).to_html( self )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
Capcode-0.8.6 lib/capcode/render/haml.rb
Capcode-0.8.5 lib/capcode/render/haml.rb
Capcode-0.8.4 lib/capcode/render/haml.rb