如何在 rails 部分渲染 sass? [英] How do I render sass in a rails partial?

查看:80
本文介绍了如何在 rails 部分渲染 sass?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rails 3.2 应用程序.我正在尝试部分渲染处理过的 sass 代码.

I have a Rails 3.2 app. I'm trying to render processed sass code in a partial.

我添加了一个初始化程序来处理 scss 文件:

I have added an initializer to handle scss files:

ActionView::Template.register_template_handler :scss,
  Sass::Rails::ScssTemplate

我的 sass 文件名为 _style.scss,渲染调用如下所示:

My sass file is called _style.scss and the render call looks like:

<%= render partial: "./templates/default/style", formats: "css" %>

我收到以下错误:

undefined method `call' for Sass::Rails::ScssTemplate:Class

对我来说,这看起来 Rails 不知道如何处理 Sass 文件,但是,我的 application.css 文件中包含的任何 .scss 文件都得到了正确处理,因此,至少在这种情况下,我知道 sass 处理有效.我已经尝试了不同的扩展和格式.在某些情况下,我可以渲染视图,但不处理 sass.

To me, this looks like Rails doesn't know how to handle the Sass file, however, any .scss files included in my application.css file are processed correctly so, at least in that context, I know sass processing works. I've experimented with different extensions and formats. In some cases I can get the view to render, but sass is not processed.

推荐答案

我创建了一个辅助方法来执行此操作

I created a helper method to do this

def render_scss(file)
  text = render("/amp/#{file}")
  view_context = controller.view_context

  engine = Sass::Engine.new(text, {
    syntax: :scss, cache: false, read_cache: false, style: :compressed,
    sprockets:  {
      context:     view_context,
      environment: view_context.assets_environment
    }
  })
  raw engine.render
end

(注意:这包括能够处理 sass 中的资产管道的链轮上下文).

(Note: this includes the sprockets context to be able to handle the asset pipeline within your sass).

然后你这样称呼它(scss 保存在名为 _amp.css.scss 的同一目录中的一部分中 - 不在资产目录中):

And then you call it like so (the scss is kept in a partial in the same directory called _amp.css.scss - not in the assets directory):

<style>
  <%= render_scss("amp.css.scss") %>
</style>

(显然这适用于 scss - 但很容易更改以应用于 sass)

(Obviously this works for scss - but it's easy to change to apply to sass)

def render_sass(file)
  text = render("/amp/#{file}")
  view_context = controller.view_context

  engine = Sass::Engine.new(text, {
    syntax: :sass, cache: false, read_cache: false, style: :compressed,
    sprockets:  {
      context:     view_context,
      environment: view_context.assets_environment
    }
  })
  raw engine.render
end

这种方法的问题在于它在运行时解析,但是这可以通过使用标准的 Rails 缓存来解决.

The problem with this approach is that it it parsed at run time, however this can be got around by using standard Rails caching.

例如

<% cache(my_object) do %>
  <style>
    <%= render_scss("amp.css.scss") %>
  </style>
<% end %>

注意:在大多数正常情况下,您需要将样式表保存在单独的文件中.但是,在某些情况下这是不合适的.例如,我正在使用这种技术来构建 Google AMP 页面,其中他们禁止将样式表放在单独的页面中文件.

Note: in most normal situations you'll want to keep your stylesheets in a separate file. However there can be situations where this isn't appropriate. For example, I'm using this technique to build Google AMP pages where they forbid the stylesheet to be in a separate file.

这篇关于如何在 rails 部分渲染 sass?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆