gpt4 book ai didi

ruby-on-rails - 在 Rails 中,如何将 Assets 管道中的样式表作为 <style> 标记包含在内?

转载 作者:行者123 更新时间:2023-12-04 20:45:39 25 4
gpt4 key购买 nike

大家都知道怎么用stylesheet_link_tag链接到样式表,但我想在页面本身中实际包含整个样式表。 (不,这通常不是一个很好的做法,但在这种情况下它是有道理的。)
stylesheet_include_tag不存在,一个在 Rails 比我更坏的同事说没有简单的方法。

问题:
是否真的可以利用 Assets 管道并将 CSS 或 JavaScript 文件(从 Sass 或 CoffeeScript 编译!)的内容嵌入到 .haml 中?看法?如何?

已添加 为清楚起见:

我希望我的布局能够包含以下内容:

= stylesheet_link_tag "base"
= stylesheet_embed_tag "page-specific-styles/foo"

并让它沿着这些行生成输出 HTML:
<link rel="stylesheet" href="/base.css" />
<style type="text/css">.foo { color: red; }</style>

更新

如果您 set your initalizer correctly,则可以在 Haml 中使用 Sass ,但我似乎不能 @import "foo"从这个上下文,其中 foo.css.sass是 Assets 管道中的样式表。请注意 @import "compass" (假设您有指南针 gem )确实有效。

这看起来像(haml):
%style
:sass
@import "foo"

尽管 Rails 声称在 app/assets/stylesheets 中查找,但它给出了找不到“foo”的错误。 (这是 foo.css.sass 住的地方)。

所以,这感觉更近了,但仍然不完全。

最佳答案

来自 http://blog.phusion.nl/2011/08/14/rendering-rails-3-1-assets-to-string/ :
YourApp::Application.assets.find_asset('api.css').source
在哈姆勒

%style
=raw YourApp::Application.assets.find_asset('foo.css').source

警告:

我相信这需要在生产中进行 Assets 编译,这可能非常昂贵。

关于ruby-on-rails - 在 Rails 中,如何将 Assets 管道中的样式表作为 &lt;style&gt; 标记包含在内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18346153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com