angelodlfrtr / mjml-ruby

Mjml ruby parser
https://mjml.io/
MIT License
11 stars 0 forks source link

Helpers in .mjml templates? #1

Open sighmon opened 8 years ago

sighmon commented 8 years ago

I'd like to be able to use ActionController::Base.helpers and route helpers in my_template.mjml.

For example

<%= image_url("asset_name.png") %>

Throws the error:

undefined method `image_url' for #<Mjml::Rails::ERBContext:0x00...>

I'd also like to use:

<%= link_to @model.title, model_url(@model) %>

Should the helpers be included in https://github.com/angelodlfrtr/mjml-ruby/blob/master/lib/mjml/rails/erb_context.rb ?

sighmon commented 8 years ago

I've made an initialiser for now..

# config/initializers/mjml.rb
class Mjml::Rails::ERBContext
  include ActionView::Helpers
  include ::Rails.application.routes.url_helpers
  default_url_options[:host] = Rails.env.production? ? ENV['APP_HOST'] : 'localhost:3000'
end
sighmon commented 8 years ago

I couldn't get partials to work either, so I ended up writing a Rails Gem: https://github.com/sighmon/mjml-rails