haml.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. require 'tilt/template'
  2. module Tilt
  3. # Haml template implementation. See:
  4. # http://haml.hamptoncatlin.com/
  5. class HamlTemplate < Template
  6. self.default_mime_type = 'text/html'
  7. def self.engine_initialized?
  8. defined? ::Haml::Engine
  9. end
  10. def initialize_engine
  11. require_template_library 'haml'
  12. end
  13. def prepare
  14. options = @options.merge(:filename => eval_file, :line => line)
  15. @engine = ::Haml::Engine.new(data, options)
  16. end
  17. def evaluate(scope, locals, &block)
  18. if @engine.respond_to?(:precompiled_method_return_value, true)
  19. super
  20. else
  21. @engine.render(scope, locals, &block)
  22. end
  23. end
  24. # Precompiled Haml source. Taken from the precompiled_with_ambles
  25. # method in Haml::Precompiler:
  26. # http://github.com/nex3/haml/blob/master/lib/haml/precompiler.rb#L111-126
  27. def precompiled_template(locals)
  28. @engine.precompiled
  29. end
  30. def precompiled_preamble(locals)
  31. local_assigns = super
  32. @engine.instance_eval do
  33. <<-RUBY
  34. begin
  35. extend Haml::Helpers
  36. _hamlout = @haml_buffer = Haml::Buffer.new(@haml_buffer, #{options_for_buffer.inspect})
  37. _erbout = _hamlout.buffer
  38. __in_erb_template = true
  39. _haml_locals = locals
  40. #{local_assigns}
  41. RUBY
  42. end
  43. end
  44. def precompiled_postamble(locals)
  45. @engine.instance_eval do
  46. <<-RUBY
  47. #{precompiled_method_return_value}
  48. ensure
  49. @haml_buffer = @haml_buffer.upper
  50. end
  51. RUBY
  52. end
  53. end
  54. end
  55. end