如何使用 SASS 作为 Rails 3 中视图的模板处理程序?
我遇到过根据数据库中的信息生成样式表的情况。这意味着我有一个views/users/show.css.erb,其中包含如下代码:
a {
color: <%= @user.favorite_color %>;
}
足够公平,与respond_to { |f| 完美配合f.css } 在我的控制器中。然而现在我想在我的 CSS 中使用一些 SASS 的时髦函数,比如变亮、变暗等,所以我想让 SASS 解析我的视图。然而,我无法弄清楚如何在我的视图中将 SASS 注册为模板处理程序,而无需太多丑陋的黑客攻击。我只是不明白它是如何工作的。有人尝试过这个吗?
我尝试过包含 SASS 的 Functions 模块但无济于事。
I have situation where I generate stylesheets based on information in the database. This means I have a views/users/show.css.erb which contains code like:
a {
color: <%= @user.favorite_color %>;
}
Fair enough, works perfectly with a respond_to { |f| f.css } in my controller. However now I would like to use some of SASS's sassy functions like lighten, darken etc. in my CSS, so I would like to have SASS parse my view. However I cannot figure out how to register SASS as a template handler in my views without too much ugly hacks. I just don't get how it works. Has anyone ever tried this?
I have tried including SASS's Functions module but to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不久前我计划对动态用户主题做同样的事情。当时,我发现了一个邮件列表回答 让我回到了正题:
该链接不再有效,但 SASS 常见问题解答只有一个名为 的部分,我可以使用我的 Sass 文件中控制器的变量? 回答了动态问题:
所以一般来说,SASS 并不是为每个请求动态生成 css 而设计的。相反,它旨在用作生成静态 css 文件的预编译解决方案。
I was planning of doing the same for dynamic user themes some time ago. Back then, I found a mailing list answer to a similar question to yours, by Nathan Weizenbaum (author of SASS) which got me back on the straight and narrow:
The link is not working anymore, but the SASS FAQ has only one section called Can I use a variable from my controller in my Sass file? which answer the dynamic question:
So in general, SASS is not designed for dynamic css generation on each request. Rather it is intended to be used as a pre compilation solution to generate static css files.