From 16f2729279befb1129fa3dbd9e916427f5cb323c Mon Sep 17 00:00:00 2001 From: nex3 Date: Thu, 5 Apr 2007 19:09:07 +0000 Subject: [PATCH] is_haml? gives the right result (false) for non-Haml partials. This means that form_for and stuff like that in partials should work properly. git-svn-id: svn://hamptoncatlin.com/haml/trunk@485 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/haml/engine.rb | 2 ++ lib/haml/helpers.rb | 2 +- lib/haml/helpers/action_view_mods.rb | 35 ++++++++++++++++++++-------- test/haml/helper_test.rb | 4 ++++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb index cbab0f09..d13e26b0 100644 --- a/lib/haml/engine.rb +++ b/lib/haml/engine.rb @@ -203,6 +203,7 @@ END def do_precompile push_silent <<-END def _haml_render + @haml_is_haml = true _hamlout = @haml_stack[-1] _erbout = _hamlout.buffer END @@ -266,6 +267,7 @@ END # Close all the open tags @template_tabs.times { close } + push_silent "@haml_is_haml = false" push_silent "end" end diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index ec169692..e9bd9f42 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -313,7 +313,7 @@ module Haml # also works in other ActionView templates, # where it will always return false. def is_haml? - @haml_stack ? @haml_stack.size > 0 : false + @haml_is_haml end include ActionViewExtensions if self.const_defined? "ActionViewExtensions" diff --git a/lib/haml/helpers/action_view_mods.rb b/lib/haml/helpers/action_view_mods.rb index 964a3ca5..5a92fd65 100644 --- a/lib/haml/helpers/action_view_mods.rb +++ b/lib/haml/helpers/action_view_mods.rb @@ -10,6 +10,17 @@ end if action_view_included module ActionView + class Base # :nodoc: + def render_with_haml(*args) + was_haml = is_haml? + @haml_is_haml = false + res = render_without_haml(*args) + @haml_is_haml = was_haml + res + end + alias_method_chain :render, :haml + end + # This overrides various helpers in ActionView # to make them work more effectively with Haml. module Helpers @@ -27,18 +38,22 @@ if action_view_included module FormTagHelper def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc) - if block_given? && is_haml? - oldproc = proc - proc = bind_proc do |*args| - concat "\n" - tab_up - oldproc.call(*args) - tab_down + if is_haml? + if block_given? + oldproc = proc + proc = bind_proc do |*args| + concat "\n" + tab_up + oldproc.call(*args) + tab_down + end end + res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) + "\n" + concat "\n" if block_given? && is_haml? + res + else + form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) end - res = form_tag_without_haml(url_for_options, options, *parameters_for_url, &proc) + "\n" - concat "\n" if block_given? && is_haml? - res end alias_method_chain :form_tag, :haml end diff --git a/test/haml/helper_test.rb b/test/haml/helper_test.rb index 63422930..c3199fae 100644 --- a/test/haml/helper_test.rb +++ b/test/haml/helper_test.rb @@ -101,6 +101,10 @@ class HelperTest < Test::Unit::TestCase def test_is_haml assert(!ActionView::Base.new.is_haml?) + assert_equal("true\n", render("= is_haml?")) + assert_equal("true\n", render("= is_haml?", :action_view)) + assert_equal("false", @base.render(:inline => '<%= is_haml? %>')) + assert_equal("false\n", render("= render :inline => '<%= is_haml? %>'", :action_view)) end def test_page_class