Action View 基础
Action View 模板可以通过多种方式编写。如果模板文件具有 .erb
扩展名,则它使用 erubi 模板系统,该系统可以将 Ruby 嵌入 HTML 文档中。如果模板文件具有 .builder
扩展名,则使用 Jim Weirich 的 Builder::XmlMarkup 库。
ERB
使用诸如 <% %>
、<% -%>
和 <%= %>
之类的嵌入来触发 ERB
。当需要输出时,使用 <%= %>
标记集。考虑以下用于名称的循环
<b>Names of all the people</b>
<% @people.each do |person| %>
Name: <%= person.name %><br/>
<% end %>
循环在常规嵌入标记 <% %>
中设置,并且使用输出嵌入标记 <%= %>
编写名称。请注意,这不仅仅是一个用法建议。诸如 print 或 puts 之类的常规输出函数无法与 ERB
模板配合使用。因此,这是错误的
<%# WRONG %>
Hi, Mr. <% puts "Frodo" %>
如果绝对必须从函数内部编写,请使用 concat
。
当在仅包含空格(标记除外)的行上时,<% %>
会抑制前导和尾随空格,包括尾随换行符。<% %>
和 <%- -%>
是相同的。但请注意,<%= %>
和 <%= -%>
是不同的:只有后者会删除尾随空格。
使用子模板
使用子模板可以避免繁琐的复制,并从共享模板中提取常见的显示结构。经典的示例是使用页眉和页脚(尽管 Action Pack 的方法是使用 Layouts
)
<%= render "application/header" %>
Something really specific and terrific
<%= render "application/footer" %>
如您所见,我们对渲染方法使用输出嵌入。渲染调用本身只会返回一个包含渲染结果的字符串。输出嵌入将其写入当前模板。
但您不必将自己限制为静态包含。模板可以通过使用使用常规嵌入标记定义的实例变量来共享变量。如下所示
<% @page_title = "A Wonderful Hello" %>
<%= render "application/header" %>
现在,页眉可以获取 @page_title
变量并使用它输出标题标记
<title><%= @page_title %></title>
将局部变量传递给子模板
您可以通过使用包含变量名称作为键和对象作为值的哈希来将局部变量传递给子模板
<%= render "application/header", { headline: "Welcome", person: person } %>
现在可以使用以下方法在 application/header
中访问这些变量
Headline: <%= headline %>
First name: <%= person.first_name %>
传递给子模板的局部变量可以使用 local_assigns
哈希作为哈希进行访问。这使您可以访问变量,如下所示
Headline: <%= local_assigns[:headline] %>
在不确定局部变量是否已分配的情况下,这非常有用。或者,您还可以使用 defined? headline
在使用变量之前首先检查变量是否已分配。
Template
缓存
默认情况下,Rails 会将每个模板编译为一个方法,以便渲染它。当您更改模板时,Rails 会检查文件的修改时间并在开发模式下重新编译它。
Builder
Builder 模板是 ERB
的更具编程性的替代方案。它们对于生成 XML 内容特别有用。名为 xml
的 XmlMarkup 对象会自动提供给扩展名为 .builder
的模板。
以下是一些基本示例
xml.em("emphasized") # => <em>emphasized</em>
xml.em { xml.b("emph & bold") } # => <em><b>emph & bold</b></em>
xml.a("A Link", "href" => "http://onestepback.org") # => <a href="http://onestepback.org">A Link</a>
xml.target("name" => "compile", "option" => "fast") # => <target option="fast" name="compile"\>
# NOTE: order of attributes is not specified.
任何带块的方法都将被视为 XML 标记,块中嵌套标记。例如,以下内容
xml.div do
xml.h1(@person.name)
xml.p(@person.bio)
end
将生成类似以下内容
<div>
<h1>David Heinemeier Hansson</h1>
<p>A product of Danish Design during the Winter of '79...</p>
</div>
以下是在 Basecamp 中实际使用的完整 RSS 示例
xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
xml.channel do
xml.title(@feed_title)
xml.link(@url)
xml.description "Basecamp: Recent items"
xml.language "en-us"
xml.ttl "40"
@recent_items.each do |item|
xml.item do
xml.title(item_title(item))
xml.description(item_description(item)) if item_description(item)
xml.pubDate(item_pubDate(item))
xml.guid(@person.firm.account.url + @recent_items.url(item))
xml.link(@person.firm.account.url + @recent_items.url(item))
xml.tag!("dc:creator", item.author_name) if item_has_creator?(item)
end
end
end
end
有关 Builder 的更多信息,请参阅 源代码。
属性
[R] | 查找上下文 | |
[R] | 视图渲染器 |
类公共方法
cache_template_loading() 链接
来源:显示 | 在 GitHub 上
# File actionview/lib/action_view/base.rb, line 171 def cache_template_loading ActionView::Resolver.caching? end
cache_template_loading=(值) 链接
来源:显示 | 在 GitHub 上
# File actionview/lib/action_view/base.rb, line 175 def cache_template_loading=(value) ActionView::Resolver.caching = value end
inspect() 链接
来源:显示 | 在 GitHub 上
# File actionview/lib/action_view/base.rb, line 191 def inspect "#<ActionView::Base:#{'%#016x' % (object_id << 1)}>" end
实例公共方法
_run(方法,模板,局部变量,缓冲区,add_to_stack: true,has_strict_locals: false,&block) 链接
来源:显示 | 在 GitHub 上
# File actionview/lib/action_view/base.rb, line 245 def _run(method, template, locals, buffer, add_to_stack: true, has_strict_locals: false, &block) _old_output_buffer, _old_virtual_path, _old_template = @output_buffer, @virtual_path, @current_template @current_template = template if add_to_stack @output_buffer = buffer if has_strict_locals begin public_send(method, buffer, **locals, &block) rescue ArgumentError => argument_error raise( ArgumentError, argument_error. message. gsub("unknown keyword:", "unknown local:"). gsub("missing keyword:", "missing local:"). gsub("no keywords accepted", "no locals accepted") ) end else public_send(method, locals, buffer, &block) end ensure @output_buffer, @virtual_path, @current_template = _old_output_buffer, _old_virtual_path, _old_template end
compiled_method_container() 链接
来源:显示 | 在 GitHub 上
# File actionview/lib/action_view/base.rb, line 270 def compiled_method_container raise NotImplementedError, <<~msg.squish Subclasses of ActionView::Base must implement `compiled_method_container` or use the class method `with_empty_template_cache` for constructing an ActionView::Base subclass that has an empty cache. msg end
in_rendering_context(选项) 链接
来源:显示 | 在 GitHub 上
# File actionview/lib/action_view/base.rb, line 278 def in_rendering_context(options) old_view_renderer = @view_renderer old_lookup_context = @lookup_context if !lookup_context.html_fallback_for_js && options[:formats] formats = Array(options[:formats]) if formats == [:js] formats << :html end @lookup_context = lookup_context.with_prepended_formats(formats) @view_renderer = ActionView::Renderer.new @lookup_context end yield @view_renderer ensure @view_renderer = old_view_renderer @lookup_context = old_lookup_context end