一般说来,layouts有5种:global layouts, controller layouts, shared layouts, dynamic layouts, action layouts.
假设有这样一个视图
<!-- views/projects/index.rhtml -->
<h2>Projects</h2>
<ul>
<% for project in @projects %>
<li><%= project.name %></li>
<% end %>
</ul>
1. global layouts
<!-- views/layouts/application.rhtml -->
<h1>Global Layouts</h1>
<%= yield %>
所有的controller都继承于application,因此application.rhtml会作为global layouts最先解析。
2. controller layouts
<!-- views/layouts/projects.rhtml -->
<h1>Controller Layouts</h1>
<%= yield %>
该layouts只作用于projects_controller
3. shared layouts
首先建立views/layouts/admin.rhtml,然后在controller中声明即可,可在多个controller中共享。
class ProjectsController < ApplicationController
layout "admin"
def index
@projects = Project.find(:all)
end
end
4. dynamic layouts
我们可以根据需要为不同的用户选择不同的layouts,比如区别admin和user。同样可以用于博客主题的替换。
class ProjectsController < ApplicationController
layout :user_layout
def index
@projects = Project.find(:all)
end
protected
def user_layout
if current_user.admin?
"admin"
else
"application"
end
end
end
5. action layouts
在action中指定layouts即可。
class ProjectsController < ApplicationController
def index
@projects = Project.find(:all)
render :layout => 'projects'
end
end
另外我们可以直接指定不使用任何layouts
class ProjectsController < ApplicationController
def index
@projects = Project.find(:all)
render :layout => false
end
end
这5种layouts的优先级为最里面的最高,也就是说action > dynamic > shared > controller > global.
分享到:
相关推荐
ADF培训_12_Building_layouts_2009
SharePoint 2013 页面访问,Url中间多一段_layouts15start#
Motivation Example Our Solution——Maze Demo 开发生成的障碍之一是内存布局操作。大量内存损坏漏洞(例如堆溢出和UAF)只能在特定的内存布局中使用。在实践中,它需要人工操作内存布局。因此,自动内存布局操作是...
bnmd6r
Thymeleaf Examples: Layouts This is an example project containing code used in the "Thymeleaf Layouts" tutorial. The project was created using Spring MVC 4 Quickstart Maven archetype: ...
Keyboard_Layouts:我的键盘布局的集合
Construction of complex layouts on Android, major layouts available on the platform, as nest them to permit more complex graphical structures are used and how to extend them to give more ...
CursoKotlin_Layouts:添加TextView
Android-Layouts part
android 窗口布局 layouts 下的各种控件 RelativeLayout gridLayout linerLayout frameLayout
客户端变量 将您的rails变量导出到客户端 安装 将此行添加到您的应用程序的Gemfile中: gem 'client_variable' 安装并将其添加到Gemfile后,需要运行生成器: ...将此行添加到app/views/layouts/ap
微信面板程序的界面布局demo,来源于网络,很漂亮
Data Page Layouts for Relational Databases Data Page Layouts for Relational Databases
Keymagic_Layouts 缅甸语言的 Keymagic 键盘布局 Keymagic 键盘布局文件 对于开发者 创建您自己的 Keymagic 键盘布局 使用 kEditor 打开。 编辑你想要的并编译它 对于用户 在您的计算机中安装 Keymagic 键盘管理器...
ClientOnClickScriptContainingPrefixedUrl="if (LaunchCreateHandler('Page')) { OpenCreateWebPageDialog('~site/_layouts/createwebpage.aspx') }" PermissionsString="AddListItems, ...
版面jekyll模板。
ExtJS-Layouts.pdf
AprilTag
#agdq15-layouts 这是一个包。 您需要安装 NodeCG 才能运行它。 这些都是在 Awesome Games Done Quick 2015 中使用的所有在线图形。安装安装到nodecg/bundles/agdq15-layouts用法此捆绑包不打算逐字使用。 许多资产...
#sgdq15-layouts这是一个捆绑包。 您将需要安装NodeCG才能运行它。 这些都是在Summer Games Done Quick 2015期间使用的所有流媒体图形。 安装 安装到nodecg/bundles/sgdq15-layouts 用法 此捆绑包不能逐字使用。 ...