Sha256: 97a9691abc5ffdcff2428af6939c0c7dc5f69228241eaf0b95a6cde2926b00bc

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

require 'malt/engines/abstract'

module Malt::Engine

  # Builder
  #
  #   http://builder.rubyforge.org/
  #
  class Builder < Abstract

    default :builder

    #
    #def intermediate(params)
    #end

    #
    def render(params, &yld)
      into = params[:to]
      case into
      when :html, nil
        render_html(params, &yld)
      else
        super(params, &yld)
      end
    end

    # TODO: Do we need a #make_ivar(data, &yld) method to make data into
    # instance variables for some templates like this one?
    def render_html(params={}, &yld)
      text = params[:text]
      file = params[:file]
      data = params[:data]

      data = make_hash(data, &yld)
      builder = ::Builder::XmlMarkup.new(engine_options(params))
      data.each{ |k,v| builder.instance_eval("@#{k} = v") }
      builder.instance_eval(text, file)
    end

    private

    # Load Erector library if not already loaded.
    def initialize_engine
      return if defined? ::Builder
      require_library 'builder'
    end

    # :target=>target_object: Object receiving the markup. target_object must
    # respond to the <<(a_string) operator and return itself.
    # The default target is a plain string target.
    #
    # :indent=>indentation: Number of spaces used for indentation. The default
    # is no indentation and no line breaks.
    #
    # :margin=>initial_indentation_level: Amount of initial indentation
    # (specified in levels, not spaces). 
    def engine_options(params)
      target = params[:target] || settings[:target]
      indent = params[:indent] || settings[:indent]
      margin = params[:margin] || settings[:margin]
      opts = {}
      opts[:target] = target if target
      opts[:indent] = indent if indent
      opts[:margin] = margin if margin
      opts
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

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