From a3388f9873b3920ee5481a3c0cc182fbe7b4dd9d Mon Sep 17 00:00:00 2001
From: Nathan Weizenbaum
Date: Sat, 23 Feb 2008 01:37:35 -0800
Subject: [PATCH] Evaluating the ERB filter in the template context as well.
---
lib/haml.rb | 3 +--
lib/haml/filters.rb | 5 +++--
test/haml/results/filters.xhtml | 26 ++++++++++++++++----------
test/haml/templates/filters.haml | 13 +++++++++----
4 files changed, 29 insertions(+), 18 deletions(-)
diff --git a/lib/haml.rb b/lib/haml.rb
index ec43e202..64b9b208 100644
--- a/lib/haml.rb
+++ b/lib/haml.rb
@@ -564,8 +564,7 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
#
# [erb] Parses the filtered text with ERB, like an RHTML template.
# Not available if the suppress_eval option is set to true.
-# At the moment, this doesn't support access to variables
-# defined by Ruby on Rails or Haml code.
+# Embedded Ruby code is evaluated in the same context as the Haml template.
#
# [sass] Parses the filtered text with Sass to produce CSS output.
#
diff --git a/lib/haml/filters.rb b/lib/haml/filters.rb
index 8205186a..dd149dbd 100644
--- a/lib/haml/filters.rb
+++ b/lib/haml/filters.rb
@@ -191,8 +191,9 @@ module Haml
include Base
lazy_require 'erb'
- def render(text)
- ::ERB.new(text).result(binding)
+ def compile(precompiler, text)
+ src = ::ERB.new(text).src.sub(/^_erbout = '';/, "").gsub("\n", ';')
+ precompiler.send(:push_silent, src)
end
end
diff --git a/test/haml/results/filters.xhtml b/test/haml/results/filters.xhtml
index 43c0b713..f022b68e 100644
--- a/test/haml/results/filters.xhtml
+++ b/test/haml/results/filters.xhtml
@@ -36,26 +36,32 @@ This pre is pretty deeply
nested.
Does interpolation work
This one is, too.
Nested, that is.
+
- a
+ - a
- b
+ - b
- c
+ - c
- d
+ - d
- e
+ - e
- f
+ - f
- g
+ - g
- h
+ - h
- i
+ - i
- j
+ - j
+
+
+
+
+178
- Foo
- Bar
diff --git a/test/haml/templates/filters.haml b/test/haml/templates/filters.haml
index c1af6436..fa96b875 100644
--- a/test/haml/templates/filters.haml
+++ b/test/haml/templates/filters.haml
@@ -52,10 +52,15 @@
This one is, too.
Nested, that is.
-:erb
- <% 10.times do |c| %>
- <%= (c+97).chr %>
- <% end %>
+- num = 10
+%ul
+ :erb
+ <% num.times do |c| %>
+ - <%= (c+97).chr %>
+ <% end %>
+ <% res = 178 %>
+
+.res= res
:markdown
* Foo