diff options
author | Peter Schilling <peter@peterschilling.org> | 2016-09-30 10:55:38 -0700 |
---|---|---|
committer | Peter Schilling <peter@peterschilling.org> | 2016-10-02 00:21:17 -0700 |
commit | f9960f2d74b510e0d994d49377cec36301f7e7f0 (patch) | |
tree | 82f369d688faafaaeaa559a6847dc6fee3edec5c /actionview/lib/action_view | |
parent | 72f97e281059bc983eef5bc8915e53249c623dff (diff) | |
download | rails-f9960f2d74b510e0d994d49377cec36301f7e7f0.tar.gz rails-f9960f2d74b510e0d994d49377cec36301f7e7f0.tar.bz2 rails-f9960f2d74b510e0d994d49377cec36301f7e7f0.zip |
Change render to support any hash keys in locals
this lets you pass ruby keywords to templates:
<%= render 'example', class: "cool" %>
<%= render 'example', "spaces are" => "a-ok" %>
<%= render 'example', Foo: "bar" %>
Previously you'd see confusing syntax errors like this:
SyntaxError (.../_example.html.erb:1: syntax error, unexpected '='
Now you can reference invalid identifiers through local_assigns.
If you try to use an invalid keyword (e.g. class) in your template, you
get a syntax error on the line where you use it.
Diffstat (limited to 'actionview/lib/action_view')
-rw-r--r-- | actionview/lib/action_view/template.rb | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index 513935cef0..c01dd1c028 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -1,5 +1,6 @@ require "active_support/core_ext/object/try" require "active_support/core_ext/kernel/singleton_class" +require "active_support/core_ext/module/delegation" require "thread" module ActionView @@ -324,8 +325,13 @@ module ActionView end def locals_code #:nodoc: + # Only locals with valid variable names get set directly. Others will + # still be available in local_assigns. + locals = @locals.to_set - Module::DELEGATION_RESERVED_METHOD_NAMES + locals = locals.grep(/\A(?![A-Z0-9])(?:[[:alnum:]_]|[^\0-\177])+\z/) + # Double assign to suppress the dreaded 'assigned but unused variable' warning - @locals.each_with_object("") { |key, code| code << "#{key} = #{key} = local_assigns[:#{key}];" } + locals.each_with_object("") { |key, code| code << "#{key} = #{key} = local_assigns[:#{key}];" } end def method_name #:nodoc: |