将 css(带变量)打印到文件
Print css (with variables) to a file
我有一个生成 epub 的服务,因此我需要生成一个 css 文件以嵌入到 epub 中。
我在 epub/style.css.erb
下创建了一个 css 视图,但是由于逻辑在服务中而不是在控制器中,所以似乎无法调用 render_to_string
. Actually, the service is wrapped in a sidekiq job, which can be called also from an after_update
from the model, so this is of further hindrance to render_to_string
.
我可以像 EpubController
一样创建并从那里调用 render_to_string
方法。但是从我位于 app/services/epub_export.rb
下的服务,我如何向控制器发送参数/从控制器接收输出?这样看起来我会破坏 MVC 模式。
但我也不喜欢经典的方式,例如
File.open(@css_path, 'w') do |f|
f.puts "@charset utf-8;"
f.puts "/* Styles for GEPUB Sample Book */"
f.puts "h1"
f.puts "{"
f.puts " text-align: center;"
f.puts " color: #0000ff;"
f.puts " font-weight: normal;"
f.puts " font-family: #{@font_family};"
f.puts "}"
end
或者有替代方案吗?
我一直在控制器外的异步作业中使用 render_to_string
来生成 pdf。看起来像:
class FooService
...
def pdf
ActionController::Base.new.render_to_string(
pdf: "pdf_name",
template: 'path/to/template',
locals: {presenter: self},
page_size: 'Letter',
encoding: "UTF-8",
margin: {
top: 20,
bottom: 20
}
)
end
...
end
你试过这些方法了吗?
我有一个生成 epub 的服务,因此我需要生成一个 css 文件以嵌入到 epub 中。
我在 epub/style.css.erb
下创建了一个 css 视图,但是由于逻辑在服务中而不是在控制器中,所以似乎无法调用 render_to_string
. Actually, the service is wrapped in a sidekiq job, which can be called also from an after_update
from the model, so this is of further hindrance to render_to_string
.
我可以像 EpubController
一样创建并从那里调用 render_to_string
方法。但是从我位于 app/services/epub_export.rb
下的服务,我如何向控制器发送参数/从控制器接收输出?这样看起来我会破坏 MVC 模式。
但我也不喜欢经典的方式,例如
File.open(@css_path, 'w') do |f|
f.puts "@charset utf-8;"
f.puts "/* Styles for GEPUB Sample Book */"
f.puts "h1"
f.puts "{"
f.puts " text-align: center;"
f.puts " color: #0000ff;"
f.puts " font-weight: normal;"
f.puts " font-family: #{@font_family};"
f.puts "}"
end
或者有替代方案吗?
我一直在控制器外的异步作业中使用 render_to_string
来生成 pdf。看起来像:
class FooService
...
def pdf
ActionController::Base.new.render_to_string(
pdf: "pdf_name",
template: 'path/to/template',
locals: {presenter: self},
page_size: 'Letter',
encoding: "UTF-8",
margin: {
top: 20,
bottom: 20
}
)
end
...
end
你试过这些方法了吗?