Mar*_*ing 5 ruby layout erb sinatra
我正在编写一个基于Sinatra的小应用程序,并希望每个视图能够将各种项目插入到布局中,例如页面title或其他CSS/javascript引用head.
目前我的布局(erb)看起来像这样(简化):
<html>
<head>
<title>Hard Coded Title Here</title>
<link rel="stylesheet" ... />
</head>
<body>
<h1>Hard Coded Title Here</h1>
<div id="content">
<%= yield %>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我不想将标题和CSS/JS引用硬编码,而是希望通过以下方式实现:
<html>
<head>
<title><%= yield :title %></title>
<link rel="stylesheet" ... />
<%= yield :more_head_refs %>
</head>
<body>
<h1><%= yield :title %></h1>
<div id="content">
<%= yield %>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
并且能够在每个视图中定义这些块的内容.
这是可能的,如果是这样,我该如何去做呢?
我最近在 Railscamp 中遇到了这个问题,幸运的是 Tim Lucas 能够向我指出他分叉并致力于的一个名为sinatra-content-for的东西。这将满足您的需求。
| 归档时间: |
|
| 查看次数: |
3750 次 |
| 最近记录: |