Jump to content

PageMjmlToHtml


monollonom
 Share

Recommended Posts

PageMjmlToHtml

Github: https://github.com/eprcstudio/PageMjmlToHtml
Modules directory: https://processwire.com/modules/page-mjml-to-html/

A module allowing you to write your Processwire template using MJML and get a converted HTML output using MJML API.

Capture d’écran 2021-12-02 à 19 05 54

About

Created by Mailjet, MJML is a markup language making it a breeze to create newsletters displayed consistently across all email clients.

Write your template using MJML combined with Processwire’s API and this module will automatically convert your code into a working newsletter thanks to their free-to-use Rest API.

Prerequisite

For this module to work you will need to get an API key and paste it in the module’s configuration.

Usage

Once your credentials are validated, select the template(s) in which you’re using the MJML syntax, save and go visualize your page(s) to see if everything’s good. You will either get error/warning messages or your email properly formatted and ready-to-go.

From there you can copy/paste the raw generated code in an external mailing service or distribute your newsletter using ProMailer.

Features

  • The MJML output is cached to avoid repetitive API calls
    • Not cached if there are errors/warnings
    • Cleared if the page is saved
    • Cleared if the template file has been modified
  • A simple (dumb?) code viewer highlights lines with errors/warnings
  • A button is added to quickly copy the raw code of the generated newsletter
    • Not added if the page is rendered outside of a PageView
    • Only visible to users with the page’s edit permission
  • A shortcut is also added under “View” in the edit page to open the raw code in a new tab
  • Multi-languages support

Capture d’écran 2021-12-02 à 21 21 45

Notes

The code viewer is only shown to superusers. If there’s an error the page will display:

  • Only its title for guests
  • Its title and a message inviting to contact the administrator for editors

If you are using the markup regions output strategy, it might be best to not append files to preserve your MJML markup before calling the MJML API. This option is available in the module’s settings.

If your layout looks weird somehow, try disabling the minification in the options.

  • Like 12
  • Thanks 4
Link to comment
Share on other sites

As this module was just recommend to me I had to give it a try.

Got the necessary API keys and grabbed the latest copy from Github. The result so far:

2021-12-03_14-55.thumb.png.522ed3fab5b6a770a535f4b66ece8342.png

Double checked the entered credentials, picked some templates and ...

2021-12-03_14-55_1.thumb.png.8d9bb6ec3b123e2ead4ae8618b45380d.png

 

I'm running the latest PW 3.0.189 here and have to remove the module to bring the site back to live.

  • Like 1
Link to comment
Share on other sites

The first part with the errors on initial save has been solved but I still need to find an alternative for the second part. In more recent dev versions Ryan removed a function I was relying on in my code (namely ProcessPageView::getPage) and I need to look for an alternative.

I'll publish an update once it's all good.

  • Like 3
Link to comment
Share on other sites

That looks way better and works almost as expected... at least in the frontend.

2021-12-03_23-12.thumb.png.93afbdc1f1f29ae613cb081eeda48e25.png

Trying the EDIT page option results in an error.

2021-12-03_23-12_1.thumb.png.54e402cc040249b40fe73d454fe12197.png

Full error:

Spoiler
DEBUG MODE BACKTRACE ($config->debug == true):
#0 /home/aca/www/pw.test/wire/core/Page.php(1808): ProcessWire\Wire->___callUnknown()
#1 /home/aca/www/pw.test/wire/core/Wire.php(420): ProcessWire\Page->___callUnknown()
#2 /home/aca/www/pw.test/wire/core/WireHooks.php(951): ProcessWire\Wire->_callMethod()
#3 /home/aca/www/pw.test/wire/core/Wire.php(485): ProcessWire\WireHooks->runHooks()
#4 /home/aca/www/pw.test/wire/core/Wire.php(488): ProcessWire\Wire->__call()
#5 /home/aca/www/pw.test/site/modules/PageMjmlToHtml-main/PageMjmlToHtml.module(444): ProcessWire\Wire->__call()
#6 /home/aca/www/pw.test/wire/core/Wire.php(417): ProcessWire\PageMjmlToHtml->addViewRaw()
#7 /home/aca/www/pw.test/wire/core/WireHooks.php(1062): ProcessWire\Wire->_callMethod()
#8 /home/aca/www/pw.test/wire/core/Wire.php(485): ProcessWire\WireHooks->runHooks()
#9 /home/aca/www/pw.test/wire/modules/Process/ProcessPageEdit/ProcessPageEdit.module(631): ProcessWire\Wire->__call()
#10 /home/aca/www/pw.test/wire/modules/Process/ProcessPageEdit/ProcessPageEdit.module(573): ProcessWire\ProcessPageEdit->renderEdit()
#11 /home/aca/www/pw.test/wire/core/Wire.php(414): ProcessWire\ProcessPageEdit->___execute()
#12 /home/aca/www/pw.test/wire/core/WireHooks.php(951): ProcessWire\Wire->_callMethod()
#13 /home/aca/www/pw.test/wire/core/Wire.php(485): ProcessWire\WireHooks->runHooks()
#14 /home/aca/www/pw.test/wire/core/ProcessController.php(337): ProcessWire\Wire->__call()
#15 /home/aca/www/pw.test/wire/core/Wire.php(414): ProcessWire\ProcessController->___execute()
#16 /home/aca/www/pw.test/wire/core/WireHooks.php(951): ProcessWire\Wire->_callMethod()
#17 /home/aca/www/pw.test/wire/core/Wire.php(485): ProcessWire\WireHooks->runHooks()
#18 /home/aca/www/pw.test/wire/core/admin.php(160): ProcessWire\Wire->__call()
#19 /home/aca/www/pw.test/site/templates/admin.php(16): require('/home/aca/www/p...')
#20 /home/aca/www/pw.test/wire/core/TemplateFile.php(327): require('/home/aca/www/p...')
#21 /home/aca/www/pw.test/wire/core/Wire.php(414): ProcessWire\TemplateFile->___render()
#22 /home/aca/www/pw.test/wire/core/WireHooks.php(951): ProcessWire\Wire->_callMethod()
#23 /home/aca/www/pw.test/wire/core/Wire.php(485): ProcessWire\WireHooks->runHooks()
#24 /home/aca/www/pw.test/wire/modules/PageRender.module(571): ProcessWire\Wire->__call()
#25 /home/aca/www/pw.test/wire/core/Wire.php(417): ProcessWire\PageRender->___renderPage()
#26 /home/aca/www/pw.test/wire/core/WireHooks.php(951): ProcessWire\Wire->_callMethod()
#27 /home/aca/www/pw.test/wire/core/Wire.php(485): ProcessWire\WireHooks->runHooks()
#28 /home/aca/www/pw.test/wire/core/WireHooks.php(1059): ProcessWire\Wire->__call()
#29 /home/aca/www/pw.test/wire/core/Wire.php(485): ProcessWire\WireHooks->runHooks()
#30 /home/aca/www/pw.test/wire/modules/Process/ProcessPageView.module(172): ProcessWire\Wire->__call()
#31 /home/aca/www/pw.test/wire/modules/Process/ProcessPageView.module(114): ProcessWire\ProcessPageView->renderPage()
#32 /home/aca/www/pw.test/wire/core/Wire.php(417): ProcessWire\ProcessPageView->___execute()
#33 /home/aca/www/pw.test/wire/core/WireHooks.php(951): ProcessWire\Wire->_callMethod()
#34 /home/aca/www/pw.test/wire/core/Wire.php(485): ProcessWire\WireHooks->runHooks()
#35 /home/aca/www/pw.test/index.php(55): ProcessWire\Wire->__call()
#36 {main}

 

  • Like 1
  • Sad 1
Link to comment
Share on other sites

I published a new update with the following changes:

  • changed filename of css/js files to PageMjmlToHtml (instead of debug)
  • changed module configuration value name templates to allowedTemplates to avoid confusion
  • modified the js so the copy to clipboard code works on all browser
  • modified getCacheName method so it also checks for LanguageSupport and LanguageSupportPageNames

Regarding point 2: please double-check the "Templates to convert (...)" field in the module settings properly catched up your previous value

Thanks again @wbmnfktr for his thorough tests!

  • Thanks 1
Link to comment
Share on other sites

I pushed a new release fixing an issue from the previous update: I forgot to update the name of the inputfield as well when switching from templates to allowedTemplates, resulting in an issue when trying to add/remove templates.

You might need to uninstall / reinstall the module, though it shouldn't be an issue to do so.

  • Like 3
Link to comment
Share on other sites

I pushed a new version with two additions:

  • the first one adds the ability to select specific roles to bypass the cache and thus convert the MJML code on each page render. I don’t think it will be used much but it’s there in case the cache invalidation when saving the page or updating the template’s file is not enough
  • the second one is more interesting as it adds the ability to generate a unique output per GET variable. Previously a GET variable would just output and save to the page’s cache but now you can specify GET variables (or the wildcard "*") to have a cached version per variable. Please note though that, when rendering, if there are several GET variables only the first one is used. `raw` is ignored as it’s used by the module. My current use-case for this is having a "browser" version (with ?browser) where I can display a newsletter with the right webfont.

I also added a small note in the settings regarding TracyDebugger to invite module users to disable the panel bar for the MJML templates. TracyDebugger hooks to `render` as well and thus some of its markup might end up in the converted code when showing the `raw` version.

I think by now this can be considered "beta", though please test on your local environment first and let me know if you notice anything.

  • Like 2
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...
On 11/21/2022 at 11:15 PM, monollonom said:

I noticed an issue with the regexes used for minifying the (working) output. I don't know why it didn't come up before but I just had the bug and fixed it.

Seeing your previous post and looking at the issue I currently face, there still seems to be something different in version 1.1.4 compared to 1.1.2.
It almost seems like my problem is the exact opposite of yours.
Is there something I'm missing here? A new setting that's not within the module settings maybe.

mjml.thumb.png.d28d597ac118c2369a128129b214229b.png

Almost identical template code but with version 1.1.4 formatted text misses quite some spaces. Even some totally valid MJML like <mj-body background-color="#efefef"> causes an issue which forced me to rewrite some wrapping to get the desired look back.

 

And of course: Happy New Year!

Link to comment
Share on other sites

Happy new year to you too @wbmnfktr !

I'm sorry you're facing this issue, most likely it's because I'm removing a space before your <strong> tag here PageMjmlToHtml.module line 161.

I'm not at home until next week and can't push a fix but you can either just comment this line and the next one or you can edit the lines so the regexes look for a space / \n only after "<" and before ">":

Quote

$mjml->html = preg_replace("/<(\n| )*/", "<", $mjml->html);

$mjml->html = preg_replace("/(\n| )*>/", ">", $mjml->html);

Hope this helps !

  • Thanks 1
Link to comment
Share on other sites

Ok, so not a setting, but Regex magic.

I'll look into it and will see if this fixes the issue. If so I might send you a pull request. It's not that urgent as the 1.1.2 runs pretty fine still.

Update:
Formatting works fine now with this change. ✓

The issue with <mj-body background-color="#efefef"> seems to come from L:366 and L:413.

  • Thanks 1
Link to comment
Share on other sites

4 minutes ago, monollonom said:

Me making bad assumptions ? 

That's what we call development here. ?

 

5 minutes ago, monollonom said:

How about changing these lines to replace "</body>" to "$view/$rawButton</body>" ? It will be safer now that I'm seeing this.

A quick try for now... and it seems to work. I will try it with the old code tomorrow and let you know.

 

6 minutes ago, monollonom said:

(my bad for not seeing the second half of your issues)

Don't worry about this! I got it fixed with a different markup already.
But so, if the fix works out well, this feature and most default MJML templates will work again.

Link to comment
Share on other sites

Okay so I published the update fixing the issues. And for your case @wbmnfktr I added an option where you can disable the minification if it's messing your layout.

By default MJML adds a lot of clutter (whitespaces) in the code and my module is pretty agressive (dumb?) in the way it handles this. So in such case, this option will help out, though you'll get a heavier code (better this way than a non-working one heh).

Please let me know if you notice any other issue.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

I've made yet another update, re: minification part + I came back to adding the debug view right after <body> like I did before, otherwise it did weird stuff to the layout. But this time I made sure my replace also handle cases where the <body> tag has an attribute, to counter the issue you had @wbmnfktr.

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

I just pushed a new release adding a new option I needed: the ability to automatically convert relative urls into absolute ones.

You can also choose the host to prepend or any query parameters to append. More infos in the settings.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...