-
Notifications
You must be signed in to change notification settings - Fork 960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert JSON MJML templates back to standard MJML #2343
Comments
You can use this helper to do so https://github.com/mjmlio/mjml/blob/master/packages/mjml-core/src/helpers/jsonToXML.js |
Awesome, thank you for the quick response! |
I reimplemented this in Ruby as follows: module Mjml
class ToXml
def initialize(source)
@source = source
end
def call
CGI.unescape_html(Nokogiri::XML::Builder.new { |xml| build_tag(xml, **@source.deep_symbolize_keys) }.to_xml)
end
def build_tag(builder, tagName:, attributes: {}, children: [], content: '', **_kwargs)
builder.public_send(tagName, CGI.unescapeHTML(content), **attributes) do
children.each { |child| build_tag(builder, **child) }
end
end
end
end |
Thank you @boardfish! |
You'd save the output of def create_local_cache_of_template(campaign)
mailer_subdir, basename = campaign.figure_out_mailer_path_somehow
mailer_dir = "#{Rails.root}/app/views/#{mailer_subdir}"
Dir.mkdir mailer_dir unless Dir.exist? mailer_dir
File.open("#{mailer_dir}/#{basename}.mjml", 'w') { |f| f.write(campaign.mjml_json) }
end ...and use mjml-rails to process it. However, unless you're also templating variables as you do in ERB, you might fall foul of sighmon/mjml-rails#85. So I actually forewent this solution in favor of using Mailjet's compiled HTML content with the regex listed in that issue. |
Thank you for the great answer, that's indeed our use case. We're evaluating options atm, from what you're saying regarding variables, it's probably better to move towards a mjml json with variables placeholders, converted to html via api or serverless function using node package, then to action mailer for sending..I just wonder about performances |
I'm working with Mailjet's templates API. Templates get returned from this in JSON format. Is there an easy way to convert from this JSON format to standard MJML?
My end goal is to compile these templates to HTML, so I might be able to draw the line from the other side and get the library I'm using to do this. But it'd be helpful to convert back to standard MJML so that it's more portable.
The text was updated successfully, but these errors were encountered: