builder.rb 858 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. require 'tilt/template'
  2. module Tilt
  3. # Builder template implementation. See:
  4. # http://builder.rubyforge.org/
  5. class BuilderTemplate < Template
  6. self.default_mime_type = 'text/xml'
  7. def self.engine_initialized?
  8. defined? ::Builder
  9. end
  10. def initialize_engine
  11. require_template_library 'builder'
  12. end
  13. def prepare; end
  14. def evaluate(scope, locals, &block)
  15. return super(scope, locals, &block) if data.respond_to?(:to_str)
  16. xml = ::Builder::XmlMarkup.new(:indent => 2)
  17. data.call(xml)
  18. xml.target!
  19. end
  20. def precompiled_preamble(locals)
  21. return super if locals.include? :xml
  22. "xml = ::Builder::XmlMarkup.new(:indent => 2)\n#{super}"
  23. end
  24. def precompiled_postamble(locals)
  25. "xml.target!"
  26. end
  27. def precompiled_template(locals)
  28. data.to_str
  29. end
  30. end
  31. end