PageRenderTime 50ms CodeModel.GetById 15ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/haml/template/patch.rb

http://github.com/scalate/scalate
Ruby | 58 lines | 39 code | 8 blank | 11 comment | 8 complexity | 9312859de26548ac02fd89ec9028b80b MD5 | raw file
 1# This file makes Haml work with Rails
 2# by monkeypatching the core template-compilation methods.
 3# This is necessary for versions <= 2.0.1 because the template handler API
 4# wasn't sufficiently powerful to deal with caching and so forth.
 5
 6# This module refers to the ActionView module that's part of Ruby on Rails.
 7# Haml can be used as an alternate templating engine for it,
 8# and includes several modifications to make it more Haml-friendly.
 9# The documentation can be found
10# here[http://rubyonrails.org/api/classes/ActionView/Base.html].
11module ActionView
12  class Base
13    def delegate_template_exists_with_haml(template_path)
14      template_exists?(template_path, :haml) && [:haml]
15    end
16    alias_method :delegate_template_exists_without_haml, :delegate_template_exists?
17    alias_method :delegate_template_exists?, :delegate_template_exists_with_haml
18
19    def compile_template_with_haml(extension, template, file_name, local_assigns)
20      return compile_haml(template, file_name, local_assigns) if extension.to_s == "haml"
21      compile_template_without_haml(extension, template, file_name, local_assigns)
22    end
23    alias_method :compile_template_without_haml, :compile_template
24    alias_method :compile_template, :compile_template_with_haml
25
26    def compile_haml(template, file_name, local_assigns)
27      render_symbol = assign_method_name(:haml, template, file_name)
28      locals = local_assigns.keys
29
30      @@template_args[render_symbol] ||= {}
31      locals_keys = @@template_args[render_symbol].keys | locals
32      @@template_args[render_symbol] = Haml::Util.to_hash(locals_keys.map {|k| [k, true]})
33
34      options = Haml::Template.options.dup
35      options[:filename] = file_name || 'compiled-template'
36
37      begin
38        Haml::Engine.new(template, options).def_method(CompiledTemplates, render_symbol, *locals_keys)
39      rescue Exception => e
40        if logger
41          logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
42          logger.debug "Backtrace: #{e.backtrace.join("\n")}"
43        end
44
45        base_path = if defined?(extract_base_path_from)
46                      # Rails 2.0.x
47                      extract_base_path_from(file_name) || view_paths.first
48                    else
49                      # Rails <=1.2.6
50                      @base_path
51                    end
52        raise ActionView::TemplateError.new(base_path, file_name || template, @assigns, template, e)
53      end
54
55      @@compile_time[render_symbol] = Time.now
56    end
57  end
58end