Processing a file by two template handlers
I'm using Rails 3.1 and I'm trying to process a file with two template handlers.
Well, I have registered a new template handler for .scss files. Now w开发者_如何学Goant to process files like this one:
app/views/custom_css/stylesheet.css.scss.erb
Through 2 template handlers. First ERB and after that SCSS. This way we can have dynamic scss files.
I tried this template handler:
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
However, in that case source equals to this:
"@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"
and I can't easily extract only "the real source".
Any ideas how this could be done?
Thank you in advance!
Doesn't the Rails 3.1 Asset Pipeline already support stacking pre-processors?
http://asciicasts.com/episodes/279-understanding-the-asset-pipeline
All you have to do is to return a string just like ERB does.
Here is my handler which inline CSS code :
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 must be wrapped by a begin-end statement. Otherwise it will raise a syntax error.
精彩评论