008 layout和content_for

If you want to change something in the layout on a per-template basis, content_for is your answer! This allows templates to specify view code that can be placed anywhere in a layout.
 
当你希望在某些模板上改变layout渲染的某个部分的话,content_for就是解决之道。它允许你在layout模板的任意地方执行特定的视图代码进行渲染。
 
首先,有一个layout文件,全局的application.rhtml:
 
<!DOCTYPE html PUBLIC...>
<html>
  <head>
           <title></title>
           <%= stylesheet_link_tag "application"%>
  </head>
  <body>
     <div id="container">
            <h1>Todo List</h1>
            <%= yield%>
     </div>
       
</html>
yield部分就是其他的譬如,index,show等等独立变现的view文件
 
 
如果现在需要index页面使用某个css文件。
 
在application.rhtml中加入:
<!DOCTYPE html PUBLIC...>
<html>
  <head>
           <title></title>
 
           <%= stylesheet_link_tag "application"%>
          <%= yield :head%>
  </head>
  <body>
     <div id="container">
            <h1>Todo List</h1>
            <%= yield%>
     </div>
       
</html>
 
在index中:
 
 
<!-- projects/index.rhtml -->
<% content_for :head do %>
    <%= stylesheet_link_tag 'projects' %>
<% end %>

<!-- layouts/application.rhtml -->
<head>
    <title>Todo List</title>
    <%= stylesheet_link_tag 'application' %>
    <%= yield :head %>
</head>

你可能感兴趣的:(layout,Rails,休闲,content_for,casts)