3

在 Rails 3.1 应用程序中,一个控制器需要使用一组自定义变量让其所有视图编译它们在每个请求中可能需要的任何 Sass 样式表。理想情况下,编译必须通过资产管道进行,以便生成基于内容的资产名称(包括内容的 MD5 散列的名称)。对于解决方案来说,使用纯 Sass 功能而不是诉诸于例如 Sass 样式表的 ERB 处理是很重要的。

根据我在这里和其他地方所做的研究,以下似乎是一种可能的方法:

  1. 设置变量访问

    • 使用自定义 Sass 函数创建某种类型的变量访问器桥,例如,如Konstantin Haase 在此处( gist ) 所述。这似乎很容易做到。

    • 通过 Sass 部分配置所有变量访问,例如_base.sassCompass 方式。部分可以使用上面定义的自定义函数。也很容易。

  2. 捕获所有资产参考

    • 装饰asset_path视图对象的方法。我有这个运作良好。

    • 使用Sprockets::Environment. 这也运作良好。

  3. 强制资产重新编译,无论文件修改时间如何

    • 我还没有找到一个好的解决方案。

    • 我已经看到了通过实例化一个新的Sass::Engine并传递将在Sass::Script::Functions::EvaluationContext. 这种方法的问题是我必须自己管理文件命名和路径,而且我总是冒着可能偏离 Sprockets 所做的事情的风险。

    • 无论文件修改时间如何,我都找不到任何基于每个请求强制 Sprockets 处理的示例,这也允许自定义变量传递。

我很感激对一般方法的评论以及关于如何最好地处理 (3) 的任何具体指针/建议。

4

2 回答 2

1

SASS is designed to be pre-compiled to CSS. Having Sprockets do this for every request for a view on a per request basis is not going to perform very well. Every request is going to have to wait for the compilation to be done, and it is not fast (from a serving-pages point of view).

The MD5 generation is within Sprockets, so if you are changing custom variables you are going to have to force a compilation on every single request to make sure that changes are seen because the view is (probably) not going to know.

It sounds as though this is not really in the sweet-spot of the asset-pipeline, and you should look at doing something more optimised for truly dynamic CSS.

Sorry. :-)

于 2011-12-05T06:02:33.093 回答
1

Sim.

It is possible. Look here SASS: Set variable at compile time

I wrote a solution to address it, I'll post soon and push it here, in case you or someone else still need it.

于 2012-07-11T13:58:44.967 回答