MRML Ruby
Ruby wrapper for MRML, the MJML markup language implementation in Rust. Rust must be available on your system to install this gem if you use a version below v1.4.2.
Installation
Add this line to your application's Gemfile:
gem 'mrml'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install mrml
Usage
require 'mrml'
mjml = "<mjml>\n<mj-head>\n<mj-title>Newsletter Title</mj-title>\n<mj-preview>Newsletter Preview</mj-preview>\n</mj-head>\n<mj-body>\n<mj-section>\n<mj-column>\n<mj-text font-size=\"20px\" color=\"#F45E43\" font-family=\"helvetica\">Hello World</mj-text>\n</mj-column>\n</mj-section>\n</mj-body>\n</mjml>\n"
# Using module methods
MRML.to_html(mjml) # Generate html from mjml
MRML.to_json(mjml) # Generate json from mjml
MRML.to_hash(mjml) # Generate hash from mjml
# Using Template class
template = MRML::Template.new(mjml)
template.title # Get template title
template.preview # Get template preview
template.to_html # Render as html
template.to_mjml # Render as mjml
template.to_json # Render as json
template.to_hash # Render as hash
require 'mrml'
json = "{\n\"type\": \"mjml\",\n\"children\": [{\n\"type\": \"mj-head\",\n\"children\": [{\n\"type\": \"mj-title\",\n\"children\": \"Newsletter Title\"\n}, {\n\"type\": \"mj-preview\",\n\"children\": \"Newsletter Preview\"\n}]\n}, {\n\"type\": \"mj-body\",\n\"children\": [{\n\"type\": \"mj-section\",\n\"children\": [{\n\"type\": \"mj-column\",\n\"children\": [{\n\"type\": \"mj-text\",\n\"attributes\": {\n\"font-size\": \"20px\",\n\"color\": \"#F45E43\",\n\"font-family\": \"helvetica\"\n},\n\"children\": [\"Hello World\"]\n}]\n}]\n}]\n}]\n}\n"
# Create Template from JSON
template = MRML::Template.from_json(json)
template.to_html # Render as html
template.to_mjml # Render as mjml
template.to_json # Render as json
template.to_hash # Render as hash
Benchmark
Warming up --------------------------------------
mrml 3.069k i/100ms
mjml 1.000 i/100ms
Calculating -------------------------------------
mrml 32.537k (
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/mrml-ruby.
License
The gem is available as open source under the terms of the MIT License.