erb.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. require 'tilt/template'
  2. module Tilt
  3. # ERB template implementation. See:
  4. # http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
  5. class ERBTemplate < Template
  6. @@default_output_variable = '_erbout'
  7. def self.default_output_variable
  8. @@default_output_variable
  9. end
  10. def self.default_output_variable=(name)
  11. @@default_output_variable = name
  12. end
  13. def self.engine_initialized?
  14. defined? ::ERB
  15. end
  16. def initialize_engine
  17. require_template_library 'erb'
  18. end
  19. def prepare
  20. @outvar = options[:outvar] || self.class.default_output_variable
  21. options[:trim] = '<>' if options[:trim].nil? || options[:trim] == true
  22. @engine = ::ERB.new(data, options[:safe], options[:trim], @outvar)
  23. end
  24. def precompiled_template(locals)
  25. source = @engine.src
  26. source
  27. end
  28. def precompiled_preamble(locals)
  29. <<-RUBY
  30. begin
  31. __original_outvar = #{@outvar} if defined?(#{@outvar})
  32. #{super}
  33. RUBY
  34. end
  35. def precompiled_postamble(locals)
  36. <<-RUBY
  37. #{super}
  38. ensure
  39. #{@outvar} = __original_outvar
  40. end
  41. RUBY
  42. end
  43. # ERB generates a line to specify the character coding of the generated
  44. # source in 1.9. Account for this in the line offset.
  45. if RUBY_VERSION >= '1.9.0'
  46. def precompiled(locals)
  47. source, offset = super
  48. [source, offset + 1]
  49. end
  50. end
  51. end
  52. # Erubis template implementation. See:
  53. # http://www.kuwata-lab.com/erubis/
  54. #
  55. # ErubisTemplate supports the following additional options, which are not
  56. # passed down to the Erubis engine:
  57. #
  58. # :engine_class allows you to specify a custom engine class to use
  59. # instead of the default (which is ::Erubis::Eruby).
  60. #
  61. # :escape_html when true, ::Erubis::EscapedEruby will be used as
  62. # the engine class instead of the default. All content
  63. # within <%= %> blocks will be automatically html escaped.
  64. class ErubisTemplate < ERBTemplate
  65. def self.engine_initialized?
  66. defined? ::Erubis
  67. end
  68. def initialize_engine
  69. require_template_library 'erubis'
  70. end
  71. def prepare
  72. @outvar = options.delete(:outvar) || self.class.default_output_variable
  73. @options.merge!(:preamble => false, :postamble => false, :bufvar => @outvar)
  74. engine_class = options.delete(:engine_class)
  75. engine_class = ::Erubis::EscapedEruby if options.delete(:escape_html)
  76. @engine = (engine_class || ::Erubis::Eruby).new(data, options)
  77. end
  78. def precompiled_preamble(locals)
  79. [super, "#{@outvar} = _buf = ''"].join("\n")
  80. end
  81. def precompiled_postamble(locals)
  82. [@outvar, super].join("\n")
  83. end
  84. # Erubis doesn't have ERB's line-off-by-one under 1.9 problem.
  85. # Override and adjust back.
  86. if RUBY_VERSION >= '1.9.0'
  87. def precompiled(locals)
  88. source, offset = super
  89. [source, offset - 1]
  90. end
  91. end
  92. end
  93. end