在rails中不同的view不同皮肤下的js,css,注入js和css到head区域

http://huobazi.aspxboy.com/blog/2010/03/30/hwo-to-injecting-themes-css-javascript-to-header-in-rails/


利用layout可以很好实现这个需求.

在ApplicationHelper内加入如下代码:

def require_js(path)
    content_for :header_js do
      include_js_tag path
    end
  end

  def require_css(path)
    content_for :header_css do
      include_css_tag path
    end
  end

  def include_js_tag(path)
    if not path.starts_with?(“http:”)
      path = “/themes/#{@setting[:theme]}/javascripts/” + path
    end
    javascript_include_tag path
  end

   def include_css_tag(path)
    if not path.starts_with?(“http:”)
      path = “/themes/#{@setting[:theme]}/stylesheets/” + path
    end
    stylesheet_link_tag path
  end

(如果你要直接在view或者layout内引入css则可以<%= include_css_tag “global.css” %>,这样生成的路径是带有皮肤目录的)

接下来,修改你的layout的head,加入如下代码:

<%= yield :header_js %>
<%= yield :header_css %>

然后在需要引入js的View内

<% require_js “jquery/jquery.tools.min.js” %>
<% require_js “jquery/jquery.colorbox.min.js” %>

最后run一下,你会看到会在html的head内生成如下html

<script src=”/themes/2010v1/javascripts/jquery/jquery.tools.min.js?1269949149” type=”text/javascript”></script>
<script src=”/themes/2010v1/javascripts/jquery/jquery.colorbox.min.js?1269949147” type=”text/javascript”></script>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值