【问题标题】:How to add a stylesheet to the layout's head of a HAML template in Sinatra?如何将样式表添加到 Sinatra 中 HAML 模板的布局头部?
【发布时间】:2013-07-05 06:18:36
【问题描述】:

我正在寻找一种将样式表链接添加到 HAML 模板的布局头部的方法。

我的布局:

!!!
  %html
    %head
      / some stuffs
    %body
      = yield

我生成的模板:

/ some other stuffs...

/maybe a function like this in order to inject 'my_stylesheet' link in layout
= content_for_head 'my_stylesheet'

有可能做这样的事情吗?

【问题讨论】:

  • 将其添加到您的脑海中:%link href="/path/to/stylesheet.css" rel="stylesheet"
  • 问题是我不希望所有共享此布局的视图都加载此样式表
  • 如何确定哪些视图获取样式表,哪些不获取?有规律吗?
  • 我希望能够在视图代码而不是布局代码中链接视图特定样式表(因为我不希望我的所有视图都加载此样式表)
  • 目前我直接在正文中链接(在我看来代码中),但这似乎是一种不好的做法

标签: sinatra haml


【解决方案1】:

有两种方法可以解决。一种是使用 Sinatra 自己的 content_for gem,或者捆绑 ActionView,这将使您可以访问 Rails 的 content_for 方法。

第二个选项是手动检查布局,并在其中包含 CSS:

# in your HAML template:
- if request.path_info == '/hello-world'
    %link{:rel => :stylesheet, :type => :"text/css", :href => "/assets/css/my_stylesheet"}

【讨论】:

    猜你喜欢
    • 2011-05-24
    • 1970-01-01
    • 2011-05-10
    • 2012-08-27
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    相关资源
    最近更新 更多