rails view session layout

[color=blue]#208 erb-blocks[/color]
简介:在erb中使用blocks,可以为 html,封装逻辑

module ApplicationHelper
def admin_area(&block)
content_tag(:div, :class => "admin", &block) if admin?
end

def admin?
true
end
end

[color=blue]#234 simple_form[/color]
<%= simple_form_for @product do |f| %>
<%= f.error_messages %>
<%= f.input :name %>
<%= f.input :price, :hint => "prices should be in USD" %>
<%= f.input :released_on %>
<%= f.association :category, :include_blank => false %>
<%= f.input :rating, :collection => 1..5, :as => :radio %>
<%= f.input :discontinued %>
<%= f.button :submit %>
<% end %>



[color=blue]#132 helpers-outside-views[/color]
1、在 model 在使用helper
def description
"This category has #{helpers.pluralize(products.count, 'product')}."
end

def helpers
ActionController::Base.helpers
end
2、在controller中使用helper
flash[:notice] = "Successfully created #{@template.link_to('product', @product)}."


[color=blue]#7 layout[/color]

Layouts are view files that define the code that surrounds a template. They can be shared across many actions and controllers.
1、默认
全局 applicateion.html.erb
controller相关 layout project.html.erb

2、动态指定
class ProjectsController < ApplicationController
layout :user_layout
protected
def user_layout
if current_user.admin?
"admin"
else
"application"
end
end
。。。
end

3、直接指定
class ProjectsController < ApplicationController
layout "admin"

def index
@projects = Project.find(:all)
render :layout => 'projects'
end

render :layout => false

end




[color=blue]#125 dynamic-layouts[/color]

简介:实现动态布局


application.rb
def load_blog
@current_blog = Blog.find_by_subdomain(current_subdomain)
if @current_blog.nil?
flash[:error] = "Blog invalid"
redirect_to root_url
else
self.class.layout(@current_blog.layout_name || 'application')
end
end
blogs_controller
1、使用application中的 load_blog
before_filter :load_blog, :only => :show

2、简单地在controller中指定
layout :blog_layout

def blog_layout
"plain"
@current_blog.layout_name
end

3、用户自定义
erviroment.rb
config.gem 'liquid'
custom.html.erb
<%= Liquid::Template.parse(@current_blog.custom_layout_content).
render('page_content' => yield, 'page_title' => yield(:title))


[color=blue]
# 8 Layouts and content_for[/color]

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.
通常,是template中的内容,通过yeild,填充到 layout中
也可以,tempate中的内容,能过content_for :title 转给 layout用,办法是 yield :title


example
template这个储存
<% content_for :head do %>
<%= stylesheet_link_tag 'projects' %>
<% end %>
layouts中这个接收
yield :title



[color=blue]#30 Pretty Page Title[/color]
Using a Helper Method

module ApplicationHelper
def title(page_title)
content_for(:title) { page_title }
end
end

template
<% title "Recent Episodes" %>

layout
<title><%= yield (:title) || "default page" %></title>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值