Skip to content

Commit ab7ae68

Browse files
committed
Add .rb template handler
This handler simply allows arbitrary Ruby code as a template
1 parent 5d264f2 commit ab7ae68

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

actionpack/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Add .rb template handler, this handler simply allows arbitrary Ruby code as a template. *Guillermo Iguaran*
4+
35
* Add `separator` option for `ActionView::Helpers::TextHelper#excerpt`:
46

57
excerpt('This is a very beautiful morning', 'very', :separator => ' ', :radius => 1)

actionpack/lib/action_view/template/handlers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def self.extended(base)
1010
base.register_default_template_handler :erb, ERB.new
1111
base.register_template_handler :builder, Builder.new
1212
base.register_template_handler :raw, Raw.new
13+
base.register_template_handler :rb, :source.to_proc
1314
end
1415

1516
@@template_handlers = {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body = ""
2+
body << ["Hello", "from", "Ruby", "code"].join(" ")
3+
body

actionpack/test/template/render_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ def test_render_raw_template_with_quotes
9797
assert_equal %q;Here are some characters: !@#$%^&*()-="'}{`; + "\n", @view.render(:template => "plain_text_with_characters")
9898
end
9999

100+
def test_render_rb_template_with_handlers
101+
assert_equal "Hello from Ruby code", @view.render(:template => "ruby_template")
102+
end
103+
104+
def test_render_rb_template_inline
105+
assert_equal '4', @view.render(:inline => "(2**2).to_s", :type => :rb)
106+
end
107+
100108
def test_render_file_with_localization_on_context_level
101109
old_locale, @view.locale = @view.locale, :da
102110
assert_equal "Hey verden", @view.render(:file => "test/hello_world")

0 commit comments

Comments
 (0)