Git Product home page Git Product logo

mjml-ruby's Introduction

MJML Ruby

Gem CI

[!] REQUIRES NODEJS

MJML parser and template engine for Ruby. Allows to create email templates without mess.

Install

Add to Gemfile:

gem 'mjml-ruby', '~> 0.4', require: 'mjml'

or

$ gem install mjml-ruby

Install NodeJS and MJML (both installations will works local and global).

$ npm install -g mjml@^3.0.0
$ bundle install

Usage

MJML v3

MJML v3 had added validation for templates and it breaks mjml-ruby v0.2.x if your template was invalid. mjml-ruby > v0.3.x has validation_level option(:soft by default) and allows to use old templates with v3. All validation errors will be logged.

Example:

MJML.configure do |config|
  config.validation_level = :soft # :skip/:soft/:strict
end

With Rails

<!-- app/views/layouts/mailer.html.mjml -->

<mjml>
  <mj-body>
    <mj-container>
      <%= yield %>
    </mj-container>
  </mj-body>
</mjml>
<!-- app/views/welcome_mailer/welcome.html.mjml -->

<mj-text>Hello, <%= @user.name %></mj-text>
class WelcomeMailer < ApplicationMailer
  def welcome(user)
    @user = user
    mail(to: @user.email, subject: 'Welcome')
  end
end

With Tilt

<!-- templates/hello.mjml -->

<mjml>
  <mj-body>
    <mj-container>
      <mj-text>Hello, world!</mj-text>
    </mj-container>
  </mj-body>
</mjml>
require 'tilt'
require 'mjml'

template = Tilt.new('templates/hello.mjml')
template.render # returns compiled HTML

With mail gem

<!-- hello.mjml -->

<mjml>
  <mj-body>
    <mj-container>
      <mj-text>Hello, world!</mj-text>
    </mj-container>
  </mj-body>
</mjml>
require 'mail'
require 'mjml'

template = File.open('hello.mjml', 'rb') { |f| MJML::Parser.new.call(f) }

Mail.deliver do
  from '[email protected]'
  to '[email protected]'
  subject 'Hello'
  body template
end

Configuration

# Change default mjml executable

# Regular Ruby
MJML.configure do |config|
  config.bin_path = '/usr/bin/env mjml'
  config.logger = YourLogger.new(STDOUT)
  config.minify_output = true
  config.validation_level = :soft
end

# Rails
Rails.application.configure do
  config.mjml.bin_path = '/usr/bin/env mjml'
  config.mjml.logger = MJML::Logger.setup!(STDOUT)
  config.mjml.minify_output = true
  config.mjml.validation_level = :soft
end

Deprecations

v0.3

  • config.debug = true is deprecated. If you are using default MJML Logger use config.logger.level = ::Logger::DEBUG instead.

TODO

  • Create parser
  • Make it configurable
  • Create Tilt interface
  • Create Sprockets interface
  • Create Railtie
  • Setup Travis
  • Add usage guide
  • Fix tests on CI
  • Add more tests
  • Improove docs

mjml-ruby's People

Contributors

alekseyg avatar doits avatar leomayleomay avatar m-basov avatar nicolas-brousse avatar stas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mjml-ruby's Issues

Improove mjml bin path finder

It's working ok on my local machine with OS X El Capitan, but fails on CI and Docker with Ruby image.

Now path is finding by function:

  def self.find_executable
    local_path = File.expand_path('node_modules/.bin/mjml', Dir.pwd)
    return local_path if File.file?(local_path)
    `/usr/bin/which mjml`.strip
  end

Compatibility with MJML v3

MJML v3.0.0 currently is not compatible with this gem. I am working on fix and also will add few options for mjml-cli. New version will have version 1.0.0 and will break compatibility with MJML v2.x.x.

MJML v3.0.0 has validation for templates by default so if your template is invalid then it will throw an error. I am working to add validation_level option that can help skip validation or log validation errors to the console. So no breaking changes and next version is v0.3.0.

Improve error message when mjml is not installed

In a server I've got an issue where mails was not sent anymore.

We've got the following error, because mjml was not installed (it was before):

ActionView::Template::Error
No such file or directory - 

I think #extract_executable_version method needs to catch Errno::ENOENT exception, or something like that.

It may help a lot for debug.

In our case, on each deploys packages are installed. But some server changes made that yarn install doesn't installed mjml.
For me the main issue here was that the error message came from ActionMailer and not MJML gem.

Previewing emails with ActionMailer::Preview doesn't parse MJML

Hi,

When we run an email with a pure ActionMailer, mjml-ruby works as expected.

On the other side, when we execute our emails via ActionMailer::Preview, the mjml is simply not parsed, but we can see the email in plain text. For example, when we visit: http://localhost:3001/rails/mailers/weekly_mailer/index

We are not sure whether this is an issue of our implementation or if there is an issue with mjml-ruby not working with ActionMailer::Preview. Could you clarify/confirm whether mjml-ruby should work with ActionMailer::Preview or not?

Thank you!

Slim support

I tried changing extensions with no luck :(

# mailers/user_mailer/receipt.html.mjml
<mj-column>
  <mj-text>
    Hello this text is inside the view
  </mj-text>
</mj-column>

Works fine. I expected this to work though:

# mailers/user_mailer/receipt.html.mjml.slim
mj-column
  mj-text Hello this text is inside the view

I get the error:

Missing template user_mailer/receipt with "mailer". Searched in:
  * "user_mailer"

Is there something special I have to do to enable other template languages. Usually with rails you can sort of chain templates indefinitely and they get passed own the chain to each successive handler.

README.md wrong syntax

in your example are some small syntax mistakes the mj-body were not closed.

<mjml>
  <mj-body>
    <mj-container>
      <%= yield %>
    </mj-container>
  <mj-body>  <%# </mj-body> %>
</mjml>

Broken in ActionMailer Preview after Rails 6.0.3 upgrade

Hi,

Thanks so much for the work on this gem. It's been incredibly useful.

I've run into an issue after upgrading my app from Rails 6.0.2.2 to 6.0.3. When working with Rails ActionMailer Previews my MJML templates are no longer transformed into HTML. Instead the template is output as raw MJML.

Everything is still working outside of Previews.

Is ActiveSupport needed?

I'm getting this on startup in a non-rails app.

NoMethodError: undefined method mattr_accessor' for Mjml::Config:Module`

Add Rails 6.0 as a Runtime dependecy or some kind of warning

Hi there, just a little feedback.

We are still on rails 5.2 for one of our apps and did not update the gem for a while. Now that we did we ran into an issue with the method ActionView::Template::Handlers::ERB.call which does not have the source parameter on rails 5.2.

So if I don't miss something this gem is only compatible with rails ~> 6.0 starting with version 0.3.5.

Adding this to your runtime dependency or some kind of warning or even an Exception when its used with rails <= 6.0 could be really helpful and save the next developer some time.

Anyway thanks a lot for providing this gem! It's much appreciated ❤️

Old dry-configurable dependency causes clashes

I have a project that uses dry-validation which is also dependent on dry-configurable, but it uses version 0.4.0 (0.7.0 is out by now). I tried testing what happens when I bump the version in a fork and it seems to work great. Is there a way we could test the gem with other versions of dry-configurable and allow a greater range of versions or to remove the dependency altogether?

Unable to parse MJML file to HTML: undefined method `empty?'

Hi, I'm trying to use mjml-ruby in a Rails app. I'm using this gem instead of mjml-rails because I only need to convert MJML to HTML, which I will plug into Brevo (formerly SendInBlue) transaction email API's html_content property. I am not using ApplicationMailer.

Following along with the Readme, the only instructions I see to convert an MJML file to HTML is on the Usage > With mail gem section. But when I run this line in a Rails console template = File.open('hello.mjml', 'rb') { |f| MJML::Parser.new.call(f) }, I get the following error:

template = File.open('app/templates/hello.mjml', 'rb') { |f| MJML::Parser.new.call(f) }
/Users/ak/.rvm/gems/ruby-3.2.2/gems/mjml-ruby-0.4.1/lib/mjml/parser.rb:29:in `exec!': undefined method `empty?' for #<File:app/templates/hello.mjml (closed)> (NoMethodError)

      raise InvalidTemplate if @template.empty?
                                        ^^^^^^^

Are you able to see if I have setup something incorrectly? I believe the code is finding the file, but however @template is set, it does not respond to the empty? method. 🤔

Gemfile

gem "mjml-ruby", "~> 0.4.1", require: 'mjml'
gem "mail", "~> 2.8" # I don't think I need the mail gem just to convert MJML to HTML

config/initializers/mjml.rb

I had to change the bin_path because the output of which mjml on my machine returns /usr/local/bin/mjml

Rails.application.configure do
  config.mjml.bin_path = '/usr/local/bin/mjml'
  config.mjml.logger = MJML::Logger.setup!(STDOUT)
  config.mjml.minify_output = true
  config.mjml.validation_level = :soft
end

app/templates/hello.mjml

<mjml>
    <mj-body>
        <mj-container>
            <mj-text>Hello, world!</mj-text>
        </mj-container>
    </mj-body>
</mjml>

mjml --version

% mjml --version
mjml-core: 4.14.1
mjml-cli: 4.14.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.