LiquidMarkdown
Combines Liquid and Markdown templating for generic templating and Rails Mailers.
Installation
Add this line to your application's Gemfile:
gem 'liquid_markdown'
And then execute:
$ bundle
Or install it yourself as:
$ gem install liquid_markdown
Usage
You can use liquid_markdown
in your mailer with .liqmd
file extension
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
def welcome(user)
@user = user
@lmVariables = @user.values
mail(to: @user.email, subject: 'liquid markdown layout') do |format|
format.html
format.text
end
end
end
# app/views/user_mailer/welcome.liqmd
# Hello Admin
Below are the list of products that you purchased
<ul id="products">
<li>
<h2>{{ product.name }}</h2>
Only {{ product.price | price }}
</li>
</ul>
Thanks
------
ABC XYZ
We can compile Liquid templates manually using html
to convert into html format and text
to convert into plain text.
lm = LiquidMarkdown::Render.new("Hello {{user.profile.name}}!", {user: {profile: {name: 'Bob'}}})
lm.html # => "<p>Hello Bob!</p>"
lm.text # => "Hello Bob!"
lm = LiquidMarkdown::Render.new("# my first heading")
lm.html # => "<h1>my first heading</h1>"
lm.text # => "my first heading"
We can combine both Liquid and Markdown together, Liquid will get compiled first and then Markdown will get compiled
lm = LiquidMarkdown::Render.new("# Hello {{username | upcase}}", {username: 'Admin'})
lm.html # => "<h1>Hello ADMIN</h1>"
lm.text # => "Hello ADMIN"
Depricated (need to remove this)
We can also setup layout options to wrap result within that layout. use {{yield}}
block in your template where we can render output.
lm = LiquidMarkdown::Render.new("# Hello {{username | upcase}}", {username: 'Admin'})
lm.layout = "<html><head></head><body>{{yield}}</body></html>"
lm.html # => "<html><head></head><body><h1>Hello ADMIN</h1></body></html>"
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
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/buzzware/liquid_markdown. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.