1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

yield :foo works when using Haml::Template.

git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@99 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2006-10-22 20:21:15 +00:00
parent 6cee0a2259
commit fe1d80b6a1
4 changed files with 27 additions and 11 deletions

View file

@ -112,11 +112,11 @@ module Haml
# Processes the template and returns the resulting (X)HTML code as
# a string.
def to_html(scope = Object.new)
def to_html(scope = Object.new, &block)
@scope_object = scope
# Compile the @precompiled buffer
compile
compile &block
# Return the result string
@buffer.buffer
@ -126,6 +126,11 @@ module Haml
#Precompile each line
def do_precompile
push_silent <<-END
def _haml_render
_hamlout = @haml_stack[-1]
_erbout = _hamlout.buffer
END
@template.each_with_index do |line, index|
count, line = count_soft_tabs(line)
suppress_render = handle_multiline(count, line, index)
@ -140,6 +145,8 @@ module Haml
# Close all the open tags
@to_close_stack.length.times { close }
push_silent "end"
end
# Processes a single line of HAML. <tt>count</tt> does *not* represent the
@ -245,7 +252,7 @@ module Haml
# evaluates it in the context of <tt>@scope_object</tt>, after preparing
# <tt>@scope_object</tt>. The code in <tt>@precompiled</tt> populates
# <tt>@buffer</tt> with the compiled XHTML code.
def compile
def compile(&block)
# Set the local variables pointing to the buffer
buffer = @buffer
@scope_object.instance_eval do
@ -258,15 +265,10 @@ module Haml
end
end
@precompiled = <<END
_hamlout = @haml_stack[-1]
_erbout = _hamlout.buffer
#{@precompiled}
END
begin
# Evaluate the buffer in the context of the scope object
@scope_object.instance_eval @precompiled
@scope_object._haml_render &block
rescue Exception => e
# Get information from the exception and format it so that
# Rails can understand it.

View file

@ -36,8 +36,12 @@ module Haml
engine = Haml::Engine.new(File.read(template_file_name))
set_precompiled(template_file_name, engine.precompiled)
end
yield_proc = @view.instance_eval do
proc { |*name| instance_variable_get("@content_for_#{name.first || 'layout'}") }
end
engine.to_html(@view)
engine.to_html(@view) { |*args| yield_proc.call(*args) }
end

View file

@ -6,5 +6,11 @@
<div id='content'>
Lorem ipsum dolor sit amet
</div>
<div id='yieldy'>
Lorem ipsum dolor sit amet
</div>
<div id='nosym'>
Lorem ipsum dolor sit amet
</div>
</body>
</html>

View file

@ -3,4 +3,8 @@
%head
%body
#content
= @content_for_layout
= @content_for_layout
#yieldy
= yield :layout
#nosym
= yield