【问题标题】:Processing a file by two template handlers通过两个模板处理程序处理文件
【发布时间】:2011-11-25 09:59:56
【问题描述】:

我正在使用 Rails 3.1,我正在尝试使用两个模板处理程序处理一个文件。

好吧,我已经为 .scss 文件注册了一个新的模板处理程序。现在想处理这样的文件:

app/views/custom_css/stylesheet.css.scss.erb

通过 2 个模板处理程序。首先是 ERB,然后是 SCSS。这样我们就可以拥有动态的 scss 文件了。

我试过这个模板处理程序:

class ActionView::Template::Handlers::Sass

  def initialize options = {}
    @options = options
  end

  def erb_handler
    @erb_handler ||= ActionView::Template.registered_template_handler(:erb)
  end

  def call template
    source = erb_handler.call(template)

    <<CODE
compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments
options  = compiler.options.merge(#{@options.inspect})
Sass::Engine.new(source, options).render
CODE
  end
end

但是,在这种情况下 source 等于:

"@output_buffer = output_buffer || ActionView::OutputBuffer.new;@output_buffer.safe_concat('$background_color: \"#ff0000\";\n\n$test: ');@output_buffer.append= ( 'test' );@output_buffer.safe_concat(';\n\n.container {\n  background-color: $background_color;\n}\n');@output_buffer.to_s"

而且我不能轻易地只提取“真正的来源”。

任何想法如何做到这一点?

提前谢谢你!

【问题讨论】:

  • 您找到解决方案了吗?因为我正在尝试做类似的事情,但我无法获得“真正的”来源。

标签: ruby-on-rails erb sass


【解决方案1】:

Rails 3.1 Asset Pipeline 不是已经支持堆叠预处理器了吗?

http://asciicasts.com/episodes/279-understanding-the-asset-pipeline

【讨论】:

  • 当然,但是在资产管道中,您无权访问应用程序环境。资产作为单独和独立的请求提供。
【解决方案2】:

您所要做的就是像 ERB 一样返回一个字符串。

这是我的内联 CSS 代码的处理程序:

module EmvHandler
    def self.erb_handler
        @@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
    end

    def self.call(template)
        compiled_source = erb_handler.call(template)
        options = {
           :warn_level => Premailer::Warnings::SAFE,
           :with_html_string => true
        }
        "Premailer.new((begin;#{compiled_source};end), #{options}).to_inline_css"
    end
end

compiler_source 必须由 begin-end 语句包装。否则会引发语法错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2011-10-25
    • 2015-02-06
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多