Module: Capcode::Helpers

Defined in:
lib/capcode/render/haml.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.haml_path=(p) ⇒ Object

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



11
12
13
14
# File 'lib/capcode/render/haml.rb', line 11

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

Instance Method Details

#render_haml(f, opts = {}) ⇒ Object

:nodoc:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/capcode/render/haml.rb', line 16

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::Configuration.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