markaby.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. require 'tilt/template'
  2. module Tilt
  3. # Markaby
  4. # http://github.com/markaby/markaby
  5. class MarkabyTemplate < Template
  6. def self.builder_class
  7. @builder_class ||= Class.new(Markaby::Builder) do
  8. def __capture_markaby_tilt__(&block)
  9. __run_markaby_tilt__ do
  10. text capture(&block)
  11. end
  12. end
  13. end
  14. end
  15. def self.engine_initialized?
  16. defined? ::Markaby
  17. end
  18. def initialize_engine
  19. require_template_library 'markaby'
  20. end
  21. def prepare
  22. end
  23. def evaluate(scope, locals, &block)
  24. builder = self.class.builder_class.new({}, scope)
  25. builder.locals = locals
  26. if data.kind_of? Proc
  27. (class << builder; self end).send(:define_method, :__run_markaby_tilt__, &data)
  28. else
  29. builder.instance_eval <<-CODE, __FILE__, __LINE__
  30. def __run_markaby_tilt__
  31. #{data}
  32. end
  33. CODE
  34. end
  35. if block
  36. builder.__capture_markaby_tilt__(&block)
  37. else
  38. builder.__run_markaby_tilt__
  39. end
  40. builder.to_s
  41. end
  42. end
  43. end