- E
- I
- L
- M
- N
- R
- S
- T
常量
| NONE | = | Object.new |
| STRICT_LOCALS_REGEX | = | /\#\s+locals:\s+\((.*)\)/ |
Attributes
| [R] | 格式 | |
| [RW] | frozen_string_literal | |
| [R] | handler | |
| [R] | 标识符 | |
| [R] | variable | |
| [R] | variant | |
| [R] | virtual_path |
类公共方法
mime_types_implementation=(implementation) 链接
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 184 def mime_types_implementation=(implementation) # This method isn't thread-safe, but it's not supposed # to be called after initialization if self::Types != implementation remove_const(:Types) const_set(:Types, implementation) end end
new(source, identifier, handler, locals:, format: nil, variant: nil, virtual_path: nil) 链接
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 199 def initialize(source, identifier, handler, locals:, format: nil, variant: nil, virtual_path: nil) @source = source.dup @identifier = identifier @handler = handler @compiled = false @locals = locals @virtual_path = virtual_path @variable = if @virtual_path base = @virtual_path.end_with?("/") ? "" : ::File.basename(@virtual_path) base =~ /\A_?(.*?)(?:\.\w+)*\z/ $1.to_sym end @format = format @variant = variant @compile_mutex = Mutex.new @strict_locals = NONE @strict_local_keys = nil @type = nil end
实例公共方法
encode!() 链接
此方法负责正确设置源的编码。在此之前,我们假定源是二进制数据。如果未提供额外信息,我们假定编码与 Encoding.default_external 相同。
用户还可以通过模板第一行的注释(# encoding: NAME-OF-ENCODING)来指定编码。这可以与任何模板引擎一起使用,因为我们在将源传递给模板引擎之前会处理掉编码注释,从而留下一行空白。
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 321 def encode! source = self.source return source unless source.encoding == Encoding::BINARY # Look for # encoding: *. If we find one, we'll encode the # String in that encoding, otherwise, we'll use the # default external encoding. if source.sub!(LEADING_ENCODING_REGEXP, "") encoding = magic_encoding = $1 else encoding = Encoding.default_external end # Tag the source with the default external encoding # or the encoding specified in the file source.force_encoding(encoding) # If the user didn't specify an encoding, and the handler # handles encodings, we simply pass the String as is to # the handler (with the default_external tag) if !magic_encoding && @handler.respond_to?(:handles_encoding?) && @handler.handles_encoding? source # Otherwise, if the String is valid in the encoding, # encode immediately to default_internal. This means # that if a handler doesn't handle encodings, it will # always get Strings in the default_internal elsif source.valid_encoding? source.encode! # Otherwise, since the String is invalid in the encoding # specified, raise an exception else raise WrongEncodingError.new(source, encoding) end end
inspect() 链接
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 300 def inspect "#<#{self.class.name} #{short_identifier} locals=#{locals.inspect}>" end
local_assigns 链接
返回一个包含已定义局部变量的哈希。
给定此子模板渲染
<%= render "application/header", { headline: "Welcome", person: person } %>
您可以在子模板中使用 local_assigns 来访问局部变量
local_assigns[:headline] # => "Welcome"
local_assigns 中的每个键都可以作为部分局部变量使用
local_assigns[:headline] # => "Welcome" headline # => "Welcome"
由于 local_assigns 是一个 Hash,它与 Ruby 3.1 的模式匹配赋值运算符兼容
local_assigns => { headline:, **options } headline # => "Welcome" options # => {}
模式匹配赋值还支持变量重命名
local_assigns => { headline: title } title # => "Welcome"
如果模板引用了一个未作为 locals: { ... } Hash 的一部分传递到视图的变量,则模板将引发 ActionView::Template::Error
<%# => raises ActionView::Template::Error %> <% alerts.each do |alert| %> <p><%= alert %></p> <% end %>
由于 local_assigns 返回一个 Hash 实例,您可以有条件地读取变量,然后在键不是 locals: { ... } 选项一部分时回退到默认值
<% local_assigns.fetch(:alerts, []).each do |alert| %> <p><%= alert %></p> <% end %>
将 Ruby 3.1 的模式匹配赋值与 +Hash#with_defaults+ 调用结合使用,可以实现紧凑的部分局部变量赋值
<% local_assigns.with_defaults(alerts: []) => { headline:, alerts: } %>
<h1><%= headline %></h1>
<% alerts.each do |alert| %>
<p><%= alert %></p>
<% end %>
默认情况下,模板将接受任何 locals 作为关键字参数,并将它们提供给 local_assigns。要限制 local_assigns 模板可以接受的内容,请添加 locals: 魔术注释
<%# locals: (headline:, alerts: []) %> <h1><%= headline %></h1> <% alerts.each do |alert| %> <p><%= alert %></p> <% end %>
在指南的 Action View 概述 中了解有关严格局部变量的更多信息。
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 165 eager_autoload do autoload :Error autoload :RawFile autoload :Renderable autoload :Handlers autoload :HTML autoload :Inline autoload :Types autoload :Sources autoload :Text autoload :Types end
locals() 链接
此模板已编译或将要编译的局部变量,如果这是严格局部变量模板,则为 nil。
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 223 def locals if strict_locals? nil else @locals end end
render(view, locals, buffer = nil, implicit_locals: [], add_to_stack: true, &block) 链接
渲染模板。如果模板尚未编译,则在其渲染之前立即进行。
此方法被内省为“!render_template.action_view”。请注意,我们在内省中使用感叹号是因为您不想在生产环境中消耗它。如果有人正在监听,它只会很慢。
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 271 def render(view, locals, buffer = nil, implicit_locals: [], add_to_stack: true, &block) instrument_render_template do compile!(view) if strict_locals? && @strict_local_keys && !implicit_locals.empty? locals_to_ignore = implicit_locals - @strict_local_keys locals.except!(*locals_to_ignore) end if buffer view._run(method_name, self, locals, buffer, add_to_stack: add_to_stack, has_strict_locals: strict_locals?, &block) nil else result = view._run(method_name, self, locals, OutputBuffer.new, add_to_stack: add_to_stack, has_strict_locals: strict_locals?, &block) result.is_a?(OutputBuffer) ? result.to_s : result end end rescue => e handle_render_error(view, e) end
short_identifier() 链接
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 296 def short_identifier @short_identifier ||= defined?(Rails.root) ? identifier.delete_prefix("#{Rails.root}/") : identifier end
source() 链接
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 304 def source @source.to_s end
strict_locals!() 链接
此方法负责将模板标记为使用严格局部变量,这意味着模板只能接受魔术注释中定义的局部变量。例如,如果您的模板接受局部变量 title 和 comment_count,请将以下内容添加到您的模板文件中
<%# locals: (title: "Default title", comment_count: 0) %>
严格局部变量对于验证模板参数和指定默认值非常有用。
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 366 def strict_locals! if @strict_locals == NONE self.source.sub!(STRICT_LOCALS_REGEX, "") @strict_locals = $1 return if @strict_locals.nil? # Magic comment not found @strict_locals = "**nil" if @strict_locals.blank? end @strict_locals end
strict_locals?() 链接
返回模板是否正在使用严格局部变量。
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 380 def strict_locals? strict_locals! end
supports_streaming?() 链接
返回底层处理程序是否支持流式传输。如果是,则在开始渲染时可能会传递流式缓冲器。
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 261 def supports_streaming? handler.respond_to?(:supports_streaming?) && handler.supports_streaming? end
translate_location(backtrace_location, spot) 链接
将 ErrorHighlight 返回的错误位置翻译成模板内的正确源位置。
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 251 def translate_location(backtrace_location, spot) if handler.respond_to?(:translate_location) handler.translate_location(spot, backtrace_location, encode!) || spot else spot end end
type() 链接
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 292 def type @type ||= Types[format] end
实例私有方法
instrument(action, &block) 链接
来源: 显示 | 在 GitHub 上
# File actionview/lib/action_view/template.rb, line 578 def instrument(action, &block) # :doc: ActiveSupport::Notifications.instrument("#{action}.action_view", instrument_payload, &block) end