sighmon / mjml-rails

MJML + ERb/Haml/Slim view template
https://mjml.io
Other
295 stars 64 forks source link

How to use this library to generate a string of raw html? #97

Open nratter opened 2 years ago

nratter commented 2 years ago

I'm looking to build a client-side 'preview' of an email using this library. Since mjml runs on the server, is there a way we can use this library to generate an html string that can then be used to pass to the client?

sighmon commented 2 years ago

@nratter For sure. Here's how I use it for that purpose:

# controller.rb

  def email
    @issue = Issue.find(params[:issue_id])

    respond_to do |format|
      format.text { render :layout => false }
      format.mjml { render :layout => false }
    end
  end
# email.mjml

<mjml>
    <mj-head>
        <mj-preview><%= "The #{@issue.release.strftime("%B %Y")} issue of New Internationalist has been published.</mj-preview>
    </mj-head>
...