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)
-
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?
-
(Boolean)
- Whether or not the format is HTML4.
def html4? @options[:format] == :html4 end
def html5?
-
(Boolean)
- Whether or not the format is HTML5.
def html5? @options[:format] == :html5 end
def html?
-
(Boolean)
- Whether or not the format is any flavor of HTML.
def html? html4? or html5? end
def initialize(template, options = {})
-
(Haml::Error)
- if there's a Haml syntax error in the template
Parameters:
-
options
({Symbol => Object}
) -- 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 doc-src/REFERENCE.md # if you update these :autoclose => %w[meta img link br hr input area param col base], :preserve => %w[textarea pre code], :filename => '(haml)', :line => 1, :ugly => false, :format => :xhtml, :escape_html => false, :escape_attrs => true, :hyphenate_data_attrs => true, } @index = nil # explicitily initialize to avoid warnings template = check_haml_encoding(template) do |msg, line| raise Haml::Error.new(msg, line) end set_up_encoding(options, template) @options.merge! options.reject {|k, v| v.nil?} @index = 0 @options[:format] = :xhtml if @options[:mime_type] == 'text/xml' unless [:xhtml, :html4, :html5].include?(@options[:format]) raise Haml::Error, "Invalid output format #{@options[:format].inspect}" 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 compile(parse) rescue Haml::Error => e if @index || e.line e.backtrace.unshift "#{@options[:filename]}:#{(e.line ? e.line + 1 : @index) + @options[:line] - 1}" end raise end
def options_for_buffer
-
({Symbol => Object})
- 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], :escape_html => @options[:escape_html], :escape_attrs => @options[:escape_attrs], :hyphenate_data_attrs => @options[:hyphenate_data_attrs], } end
def precompiled
-
(String)
-
def precompiled @precompiled end
def precompiled
def precompiled encoding = Encoding.find(@options[:encoding]) return @precompiled.force_encoding(encoding) if encoding == Encoding::BINARY return @precompiled.encode(encoding) end
def render(scope = Object.new, locals = {}, &block)
-
(String)
- The rendered template
Parameters:
-
block
(#to_proc
) -- A block that can be yielded to within the template -
locals
({Symbol => Object}
) -- 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) parent = scope.instance_variable_defined?('@haml_buffer') ? scope.instance_variable_get('@haml_buffer') : nil buffer = Haml::Buffer.new(parent, 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 + ";" + precompiled_method_return_value, scope, @options[:filename], @options[:line]) ensure # Get rid of the current buffer scope_object.instance_eval do @haml_buffer = buffer.upper if buffer end end
def render_proc(scope = Object.new, *local_names)
-
(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 set_up_encoding(options, template)
def set_up_encoding(options, template) options end
def set_up_encoding(options, template)
def set_up_encoding(options, template) @options.tap do |ops| ops[:encoding] = Encoding.default_internal || template.encoding ops[:encoding] = "utf-8" if ops[:encoding].name == "US-ASCII" ops[:encoding] = ops[:encoding].name if ops[:encoding].is_a?(Encoding) end end
def xhtml?
-
(Boolean)
- Whether or not the format is XHTML.
def xhtml? not html? end