Permalink
Browse files

cleanup of local_assigns handling and documentation update (closes #6358

) [skaes]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5231 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 0c3c131 commit 8ba8c7c560397b783743a662789fedcf85855ce2 @dhh dhh committed Oct 8, 2006
Showing with 16 additions and 6 deletions.
  1. +16 −6 actionpack/lib/action_view/base.rb
@@ -53,13 +53,22 @@ class ActionViewError < StandardError #:nodoc:
#
# You can pass local variables to sub templates by using a hash with the variable names as keys and the objects as values:
#
- # <%= render "shared/header", { "headline" => "Welcome", "person" => person } %>
+ # <%= render "shared/header", { :headline => "Welcome", :person => person } %>
#
# These can now be accessed in shared/header with:
#
# Headline: <%= headline %>
# First name: <%= person.first_name %>
#
+ # If you need to find out whether a certain local variable has been assigned a value in a particular render call,
+ # you need to use the following pattern:
+ #
+ # <% if local_assigns.has_key? :headline %>
+ # Headline: <%= headline %>
+ # <% end %>
+ #
+ # Testing using <tt>defined? headline</tt> will not work. This is an implementation restriction.
+ #
# == Template caching
#
# By default, Rails will compile each template to a method in order to render it. When you alter a template, Rails will
@@ -301,6 +310,9 @@ def render_template(template_extension, template, file_path = nil, local_assigns
# will only be read if it has to be compiled.
#
def compile_and_render_template(extension, template = nil, file_path = nil, local_assigns = {}) #:nodoc:
+ # convert string keys to symbols if requested
+ local_assigns = local_assigns.symbolize_keys if @@local_assigns_support_string_keys
+
# compile the given template, if necessary
if compile_template?(template, file_path, local_assigns)
template ||= read_template_file(file_path, extension)
@@ -311,8 +323,6 @@ def compile_and_render_template(extension, template = nil, file_path = nil, loca
method_name = @@method_names[file_path || template]
evaluate_assigns
- local_assigns = local_assigns.symbolize_keys if @@local_assigns_support_string_keys
-
send(method_name, local_assigns) do |*name|
instance_variable_get "@content_for_#{name.first || 'layout'}"
end
@@ -427,8 +437,8 @@ def compile_template?(template, file_name, local_assigns)
if @@compile_time[render_symbol] && supports_local_assigns?(render_symbol, local_assigns)
if file_name && !@@cache_template_loading
- @@compile_time[render_symbol] < File.mtime(file_name) || (File.symlink?(file_name) ?
- @@compile_time[render_symbol] < File.lstat(file_name).mtime : false)
+ @@compile_time[render_symbol] < File.mtime(file_name) ||
+ (File.symlink?(file_name) && (@@compile_time[render_symbol] < File.lstat(file_name).mtime))
end
else
true
@@ -457,7 +467,7 @@ def create_template_source(extension, template, render_symbol, locals)
locals_code = ""
locals_keys.each do |key|
- locals_code << "#{key} = local_assigns[:#{key}] if local_assigns.has_key?(:#{key})\n"
+ locals_code << "#{key} = local_assigns[:#{key}]\n"
end
"def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"

0 comments on commit 8ba8c7c

Please sign in to comment.