Skip to content

Commit

Permalink
cleanup of local_assigns handling and documentation update (closes #6358
Browse files Browse the repository at this point in the history
) [skaes]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5231 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Oct 8, 2006
1 parent 0c3c131 commit 8ba8c7c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -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: # 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: # These can now be accessed in shared/header with:
# #
# Headline: <%= headline %> # Headline: <%= headline %>
# First name: <%= person.first_name %> # 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 # == Template caching
# #
# By default, Rails will compile each template to a method in order to render it. When you alter a template, Rails will # By default, Rails will compile each template to a method in order to render it. When you alter a template, Rails will
Expand Down Expand Up @@ -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. # will only be read if it has to be compiled.
# #
def compile_and_render_template(extension, template = nil, file_path = nil, local_assigns = {}) #:nodoc: 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 # compile the given template, if necessary
if compile_template?(template, file_path, local_assigns) if compile_template?(template, file_path, local_assigns)
template ||= read_template_file(file_path, extension) template ||= read_template_file(file_path, extension)
Expand All @@ -311,8 +323,6 @@ def compile_and_render_template(extension, template = nil, file_path = nil, loca
method_name = @@method_names[file_path || template] method_name = @@method_names[file_path || template]
evaluate_assigns evaluate_assigns


local_assigns = local_assigns.symbolize_keys if @@local_assigns_support_string_keys

send(method_name, local_assigns) do |*name| send(method_name, local_assigns) do |*name|
instance_variable_get "@content_for_#{name.first || 'layout'}" instance_variable_get "@content_for_#{name.first || 'layout'}"
end end
Expand Down Expand Up @@ -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 @@compile_time[render_symbol] && supports_local_assigns?(render_symbol, local_assigns)
if file_name && !@@cache_template_loading if file_name && !@@cache_template_loading
@@compile_time[render_symbol] < File.mtime(file_name) || (File.symlink?(file_name) ? @@compile_time[render_symbol] < File.mtime(file_name) ||
@@compile_time[render_symbol] < File.lstat(file_name).mtime : false) (File.symlink?(file_name) && (@@compile_time[render_symbol] < File.lstat(file_name).mtime))
end end
else else
true true
Expand Down Expand Up @@ -457,7 +467,7 @@ def create_template_source(extension, template, render_symbol, locals)


locals_code = "" locals_code = ""
locals_keys.each do |key| 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 end


"def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend" "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
Expand Down

0 comments on commit 8ba8c7c

Please sign in to comment.