Sha256: ce7d3a391828d2f23c8cb6bd80a29e254fd7e896e045c4a4ea39ca42363fc008

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

require 'malt/engines/abstract'

module Malt::Engine

  # ERB template implementation.
  #
  #   http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
  #
  # ERB templates accept two options. +safe+ sets the safe mode for
  # rendering the template and +trim+ is a weird string that controls
  # a few rendering options --it can be '%' and/or '>' or '<>'.
  class Erb < Abstract

    default :erb, :rhtml

    # Render ERB template.
    #
    # The +params+ can be:
    #
    # * :text - text of erb document
    # * :file - file name where text was read (or nil)
    # * :data - data source for template interpolation
    # * :safe -
    # * :trim -
    #
    # Returns a String.
    def render(params={}, &yld)
      text = params[:text]
      file = params[:file]
      data = params[:data]

      data = make_binding(data, &yld)

      if settings[:precompile] == false
        intermediate(params).result(data)
      else
        ruby = compile(params)
        eval(ruby, data, file)
      end
    end

    # Compile ERB template into Ruby source code.
    def compile(params={})
      file = params[:file]
      if cache?
        @source[file] ||= intermediate(params).src
      else
        intermediate(params).src
      end
    end

    # Returns instance of underlying ::ERB class.
    def intermediate(params={})
      text = params[:text]
      file = params[:file]

      opts = engine_options(params)
      safe = opts[:safe]
      trim = opts[:trim]

      if cache?
        @cache[file] ||= ::ERB.new(text, safe, trim)
      else
        ::ERB.new(text, safe, trim)
      end
    end

    private

    # Load ERB library if not already loaded.
    def initialize_engine
      return if defined? ::ERB
      require_library('erb')
    end

    def engine_options(params)
      opts = {}
      opts[:safe] = params[:safe] || settings[:safe]
      opts[:trim] = params[:trim] || settings[:trim]
      opts
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
malt-0.3.0 lib/malt/engines/erb.rb
malt-0.2.0 lib/malt/engines/erb.rb