Skip to content
This repository
Browse code

Add .rb template handler

This handler simply allows arbitrary Ruby code as a template
  • Loading branch information...
commit ab7ae689e9727781ef39b01a836a746ac75352fc 1 parent 5d264f2
Guillermo Iguaran authored September 11, 2012
2  actionpack/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,7 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Add .rb template handler, this handler simply allows arbitrary Ruby code as a template. *Guillermo Iguaran*
  4
+
3 5
 *   Add `separator` option for `ActionView::Helpers::TextHelper#excerpt`:
4 6
 
5 7
         excerpt('This is a very beautiful morning', 'very', :separator  => ' ', :radius => 1)
1  actionpack/lib/action_view/template/handlers.rb
@@ -10,6 +10,7 @@ def self.extended(base)
10 10
         base.register_default_template_handler :erb, ERB.new
11 11
         base.register_template_handler :builder, Builder.new
12 12
         base.register_template_handler :raw, Raw.new
  13
+        base.register_template_handler :rb, :source.to_proc
13 14
       end
14 15
 
15 16
       @@template_handlers = {}
3  actionpack/test/fixtures/ruby_template.rb
... ...
@@ -0,0 +1,3 @@
  1
+body = ""
  2
+body << ["Hello", "from", "Ruby", "code"].join(" ")
  3
+body
8  actionpack/test/template/render_test.rb
@@ -97,6 +97,14 @@ def test_render_raw_template_with_quotes
97 97
     assert_equal %q;Here are some characters: !@#$%^&*()-="'}{`; + "\n", @view.render(:template => "plain_text_with_characters")
98 98
   end
99 99
 
  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
+
100 108
   def test_render_file_with_localization_on_context_level
101 109
     old_locale, @view.locale = @view.locale, :da
102 110
     assert_equal "Hey verden", @view.render(:file => "test/hello_world")

5 notes on commit ab7ae68

Joshua Peek
Collaborator

This is pretty annoying change.

If you have any .rb files under a view path, AP is going to try to precompile them.

Just put a module under app/views echo "module Foo; end" > app/views/foo.rb and your app will just crash will an error message saying "module definition in method body".

My personal usage is mustache view classes.

Guillermo Iguaran

@josh I added this since @dhh asked for it after of see it in Railscasts, I will talk with him on CF to check if we can found any solution or if we should revert this

Joshua Peek
Collaborator

I like that jbuilder (and similar extensions) use its own extension, not .rb.

David Heinemeier Hansson
Owner

We'll just go with .ruby for now, then.

Guillermo Iguaran

Done in de1060f

Please sign in to comment.
Something went wrong with that request. Please try again.