class Haml::Engine

puts output
output = haml_engine.render
haml_engine = Haml::Engine.new(template)
template = File.read(‘templates/really_cool_template.haml’)
For example:
new instance and calling {#render} to render the template.
It can be directly used by the user by creating a
This is the frontend for using Haml programmatically.

def def_method(object, name, *local_names)

Parameters:
  • local_names (Array) -- The names of the locals that can be passed to the proc
  • name (String, Symbol) -- The name of the method to define
  • object (Object, Module) -- The object on which to define the method
def def_method(object, name, *local_names)
  method = object.is_a?(Module) ? :module_eval : :instance_eval
  object.send(method, "def #{name}(_haml_locals = {}); #{precompiled_with_ambles(local_names)}; end",
              @options[:filename], @options[:line])
end

def html4?

Returns:
  • (Boolean) - Whether or not the format is HTML4.
def html4?
  @options[:format] == :html4
end

def html5?

Returns:
  • (Boolean) - Whether or not the format is HTML5.
def html5?
  @options[:format] == :html5
end

def html?

Returns:
  • (Boolean) - Whether or not the format is any flavor of HTML.
def html?
  html4? or html5?
end

def initialize(template, options = {})

Raises:
  • (Haml::Error) - if there's a Haml syntax error in the template

Parameters:
  • options (Hash) -- An options hash;
  • template (String) -- The Haml template
def initialize(template, options = {})
  @options = {
    :suppress_eval => false,
    :attr_wrapper => "'",
    # Don't forget to update the docs in lib/haml.rb if you update these
    :autoclose => %w[meta img link br hr input area param col base],
    :preserve => %w[textarea pre],
    :filename => '(haml)',
    :line => 1,
    :ugly => false,
    :format => :xhtml,
    :escape_html => false,
  }
  unless ruby1_8?
    @options[:encoding] = Encoding.default_internal || "utf-8"
  end
  @options.merge! options
  @index = 0
  unless [:xhtml, :html4, :html5].include?(@options[:format])
    raise Haml::Error, "Invalid format #{@options[:format].inspect}"
  end
  if @options[:encoding] && @options[:encoding].is_a?(Encoding)
    @options[:encoding] = @options[:encoding].name
  end
  # :eod is a special end-of-document marker
  @template = (template.rstrip).split(/\r\n|\r|\n/) + [:eod, :eod]
  @template_index = 0
  @to_close_stack = []
  @output_tabs = 0
  @template_tabs = 0
  @flat = false
  @newlines = 0
  @precompiled = ''
  @to_merge = []
  @tab_change  = 0
  @temp_count = 0
  precompile
rescue Haml::Error => e
  e.backtrace.unshift "#{@options[:filename]}:#{(e.line ? e.line + 1 : @index) + @options[:line] - 1}" if @index
  raise
end

def options_for_buffer

Returns:
  • (Hash) - The options hash
def options_for_buffer
  {
    :autoclose => @options[:autoclose],
    :preserve => @options[:preserve],
    :attr_wrapper => @options[:attr_wrapper],
    :ugly => @options[:ugly],
    :format => @options[:format],
    :encoding => @options[:encoding],
  }
end

def precompiled

Returns:
  • (String) -
def precompiled
  return @precompiled if ruby1_8?
  return @precompiled.encode(Encoding.find(@options[:encoding]))
end

def render(scope = Object.new, locals = {}, &block)

Returns:
  • (String) - The rendered template

Parameters:
  • block (#to_proc) -- A block that can be yielded to within the template
  • locals (Hash) -- Local variables that will be made available
  • scope (Binding, Proc, Object) -- The context in which the template is evaluated
def render(scope = Object.new, locals = {}, &block)
  buffer = Haml::Buffer.new(scope.instance_variable_get('@haml_buffer'), options_for_buffer)
  if scope.is_a?(Binding) || scope.is_a?(Proc)
    scope_object = eval("self", scope)
    scope = scope_object.instance_eval{binding} if block_given?
  else
    scope_object = scope
    scope = scope_object.instance_eval{binding}
  end
  set_locals(locals.merge(:_hamlout => buffer, :_erbout => buffer.buffer), scope, scope_object)
  scope_object.instance_eval do
    extend Haml::Helpers
    @haml_buffer = buffer
  end
  eval(precompiled, scope, @options[:filename], @options[:line])
  # Get rid of the current buffer
  scope_object.instance_eval do
    @haml_buffer = buffer.upper
  end
  buffer.buffer
end

def render_proc(scope = Object.new, *local_names)

Returns:
  • (Proc) - The proc that will run the template

Parameters:
  • local_names (Array) -- The names of the locals that can be passed to the proc
  • scope (Binding, Proc, Object) -- The context in which the template is evaluated
def render_proc(scope = Object.new, *local_names)
  if scope.is_a?(Binding) || scope.is_a?(Proc)
    scope_object = eval("self", scope)
  else
    scope_object = scope
    scope = scope_object.instance_eval{binding}
  end
  eval("Proc.new { |*_haml_locals| _haml_locals = _haml_locals[0] || {};" +
       precompiled_with_ambles(local_names) + "}\n", scope, @options[:filename], @options[:line])
end

def set_locals(locals, scope, scope_object)

def set_locals(locals, scope, scope_object)
  scope_object.send(:instance_variable_set, '@_haml_locals', locals)
  set_locals = locals.keys.map { |k| "#{k} = @_haml_locals[#{k.inspect}]" }.join("\n")
  eval(set_locals, scope)
end

def xhtml?

Returns:
  • (Boolean) - Whether or not the format is XHTML.
def xhtml?
  not html?
end