| Class | Haml::Template |
| In: |
lib/haml/template.rb
|
| Parent: | Object |
This class interfaces with ActionView to make Haml usable as a Ruby on Rails plugin. It usually shouldn‘t need to be used by end users. Just in case, though, here‘s what you might do to render templates/index.haml:
ActionView::Base.register_template_handler("haml", Haml::Template)
base = ActionView::Base.new("templates")
base.render("index")
Or, if you want to really get into the nitty-gritty:
base = ActionView::Base.new
template = Haml::Template.new(base)
template.render("templates/index.haml")
Creates a new Haml::Template object that uses view to render its templates.
# File lib/haml/template.rb, line 41
41: def initialize(view)
42: @view = view
43: end
Renders the file at the location template, with local_assigns available as local variables within the template. Returns the result as a string.
# File lib/haml/template.rb, line 48
48: def render(template, local_assigns={})
49: @view.instance_eval do
50: evaluate_assigns
51: end
52:
53: options = @@options.dup
54: locals = options[:locals] || {}
55: locals.merge! local_assigns
56: options[:locals] = locals
57:
58: if @view.haml_inline
59: engine = Haml::Engine.new(template, options)
60: else
61: options[:filename] ||= template
62: engine = Haml::Engine.new(File.read(template), options)
63: end
64:
65: yield_proc = @view.instance_eval do
66: proc { |*name| instance_variable_get("@content_for_#{name.first || 'layout'}") }
67: end
68:
69: engine.to_html(@view) { |*args| yield_proc.call(*args) }
70:
71: end