/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. # This module refers to the ActionView module that's part of Ruby on Rails.
  6. # Haml can be used as an alternate templating engine for it,
  7. # and includes several modifications to make it more Haml-friendly.
  8. # The documentation can be found
  9. # here[http://rubyonrails.org/api/classes/ActionView/Base.html].
  10. module ActionView
  11. class Base
  12. def delegate_template_exists_with_haml(template_path)
  13. template_exists?(template_path, :haml) && [:haml]
  14. end
  15. alias_method :delegate_template_exists_without_haml, :delegate_template_exists?
  16. alias_method :delegate_template_exists?, :delegate_template_exists_with_haml
  17. def compile_template_with_haml(extension, template, file_name, local_assigns)
  18. return compile_haml(template, file_name, local_assigns) if extension.to_s == "haml"
  19. compile_template_without_haml(extension, template, file_name, local_assigns)
  20. end
  21. alias_method :compile_template_without_haml, :compile_template
  22. alias_method :compile_template, :compile_template_with_haml
  23. def compile_haml(template, file_name, local_assigns)
  24. render_symbol = assign_method_name(:haml, template, file_name)
  25. locals = local_assigns.keys
  26. @@template_args[render_symbol] ||= {}
  27. locals_keys = @@template_args[render_symbol].keys | locals
  28. @@template_args[render_symbol] = Haml::Util.to_hash(locals_keys.map {|k| [k, true]})
  29. options = Haml::Template.options.dup
  30. options[:filename] = file_name || 'compiled-template'
  31. begin
  32. Haml::Engine.new(template, options).def_method(CompiledTemplates, render_symbol, *locals_keys)
  33. rescue Exception => e
  34. if logger
  35. logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
  36. logger.debug "Backtrace: #{e.backtrace.join("\n")}"
  37. end
  38. base_path = if defined?(extract_base_path_from)
  39. # Rails 2.0.x
  40. extract_base_path_from(file_name) || view_paths.first
  41. else
  42. # Rails <=1.2.6
  43. @base_path
  44. end
  45. raise ActionView::TemplateError.new(base_path, file_name || template, @assigns, template, e)
  46. end
  47. @@compile_time[render_symbol] = Time.now
  48. end
  49. end
  50. end