-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sprockets v3.7.0 deprecation warning #292
Comments
Sprockets was initially based on Tilt, but in the later versions they've decided to not make its interface compatible with Tilt. That is a completely fair decision as Tilt templates and Sprocket processors have different use cases. There are no plans to change Tilt to be compatible with Sprockets v4. I think you can solve the problem by writing a class which implements class TiltProcessor
def initialize(template_class)
@template_class = template_class
end
def call(input)
# not sure if I've implemented this correctly
result = @template_class.new(input[:filename]) { input[:data] }.render
{ data: result }
end
end
config.register_preprocessor 'text/html', TiltProcessor.new(Tilt::HamlTemplate) |
Thanks, @judofyr , for the code snippet. It (almost) works, just had to change the
Because for some reason Sprockets insists the data is I encountered another problem. In the haml templates I use some view helpers and used to include them like:
For some reason after using |
You could maybe do something like this: class TiltProcessor
def initialize(template_class)
@template_class = template_class
end
def context
@context ||= Object.new
end
def call(input)
result = @template_class.new(input[:filename]) { input[:data] }.render(context)
{ data: result }
end
end And then you can processor = TiltProcessor.new(Tilt::HamlTemplate)
processor.context.extend Rails.application.routes.url_helpers
config.register_preprocessor 'text/html', processor |
@judofyr, thank you very much for helping me with this! class TiltProcessor
def initialize(template_class, context = nil)
@template_class = template_class
@context = context || Object.new
end
def call(input)
result = @template_class.new(input[:filename]) { input[:data] }.render(@context)
{ data: String.new(result) }
end
end
class HamlAssetsContext
include SimpleForm::Helpers
include SimpleForm::ActionViewExtensions::FormHelper
include ActionView::Helpers
include Rails.application.routes.url_helpers
# Include any other view helper module that you need. eg:
# include ApplicationHelper
end
Rails.application.config.assets.configure do |config|
config.register_mime_type 'text/html', extensions: ['.haml', '.html.haml']
processor = TiltProcessor.new(Tilt::HamlTemplate, HamlAssetsContext.new)
config.register_preprocessor 'text/html', processor
end |
Very happy you found a solution :) I'm closing this issue now. Feel free to reopen or comment if you have any other issues/questions. (One tiny suggestion: Change the initializer to |
The above solution has some issues related with the context. For example After some more research I managed to find my way around to sprockets legacy_tilt_processor.rb. Adding the final version here, just in case this issue attracts more people with the same/similar problem. # For Rails add to a file in config/initializers
class TiltProcessor
def initialize(klass)
@klass = klass
end
def call(input)
filename = input[:filename]
data = input[:data]
context = input[:environment].context_class.new(input)
data = @klass.new(filename) { data }.render(context, {})
context.metadata.merge(data: data.to_str)
end
end
Rails.application.config.assets.configure do |env|
env.context_class.class_eval do
include SimpleForm::Helpers
include SimpleForm::ActionViewExtensions::FormHelper
include ActionView::Helpers
include Rails.application.routes.url_helpers
# Include any other view helper module that you need. eg:
# include ApplicationHelper
end
env.register_mime_type 'text/html', extensions: ['.haml', '.html.haml']
env.register_preprocessor 'text/html', TiltProcessor.new(Tilt::HamlTemplate)
end Just don't understand why the Sprockets' processor class needs to inherit |
I use haml in some asset templates. After I updated Sprockets to v3.7. I got a deprecation warning:
It seems to me this is not limited to Haml, but all template engines supported by Tilt. Is
Tilt::Template
supposed to implementself.call
in order to support Sprockets v4?The code that triggers the warning is a Rails initializer:
The text was updated successfully, but these errors were encountered: