Sha256: d96cc34a49abc39657c05c15afdad0787f2230f0969434ad2021f73afcc2f3f6

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

begin
  require 'haml'
rescue LoadError => e
  raise MissingLibrary, "Haml could not be loaded (is it installed?): #{e.message}"
end

module Capcode
  module Helpers
    # Set the path to Haml files. If this path is not set, Capcode will search in the static path.
    # This method is deprecated and will be removed in version 1.0
    def self.haml_path=( p )
      warn "Capcode::Helpers.haml_path is deprecated and will be removed in version 1.0, please use `set :haml'"
      Capcode::Configuration.set :haml, p
    end
    
    def render_haml( f, opts = {} ) #:nodoc:
      if @haml_path.nil?
        @haml_path = Capcode::Configuration.get( :haml ) || 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
      
      # Update options
      opts = (Capcode.options[:haml] || {}).merge(opts)
      
      # Get Layout file
      layout = opts.delete(:layout)||:layout
      layout_file = File.join( @haml_path, layout.to_s+".haml" )
      
      # Get HAML File
      f = f + ".haml" if File.extname( f ) != ".haml"
      file = File.join( @haml_path, f )

      # Render
      if( File.exist?( file ) )
        if( File.exist?( layout_file ) )
          Haml::Engine.new( open( layout_file ).read, opts ).to_html(self) { |*args| 
            #@@__ARGS__ = args
            Capcode::Helpers.args = args
            Haml::Engine.new( open( file ).read ).render(self) 
          }
        else
          Haml::Engine.new( open( file ).read, opts ).to_html( self )
        end
      else
        raise Capcode::RenderError, "Error rendering `haml', #{file} does not exist !"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capcode-render-haml-0.1.0 lib/capcode/render/haml.rb