Skip to content
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

How to output raw text in mjml-react? #41

Closed
kdallmeyer-sr opened this issue Oct 20, 2020 · 9 comments
Closed

How to output raw text in mjml-react? #41

kdallmeyer-sr opened this issue Oct 20, 2020 · 9 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@kdallmeyer-sr
Copy link

Hello,

We are using MGML React to output email templates that will then use handlebars when processing the final email.

For example, we have now handlebars function with named parameter format. When using React, it escapes the quote in the named parameter and breaks handlebars processing.

Neither MjmlText or MjmlRaw work.

<MjmlText>{`{{now format="YYYY"}}`}</MjmlText>
<MjmlRaw>{`{{now format="YYYY"}}`}</MjmlRaw>

outputs

<mj-text>{{now format=&#x27;YYYY&#x27;}}</mj-text>
<mj-raw>{{now format=&#x27;YYYY&#x27;}}</mj-raw>

"dangerouslySetInnerHTML" does not work either.

<MjmlText dangerouslySetInnerHTML={{ __html=`{{now format="YYYY"}}`}}/>
<MjmlText dangerouslySetInnerHTML={{ __html=`{{now format="YYYY"}}`}}/>

outputs

<mj-text dangerously-set-inner-html="[Object]"></mj-text>
<mj-raw dangerously-set-inner-html="[Object]"></mj-raw>

Any idea on how to do this in React or in MJML-React?

@mastertheblaster mastertheblaster added the question Further information is requested label Oct 20, 2020
@mastertheblaster
Copy link
Collaborator

Hey,

Try the following code:

function Raw({ html }) {
  return React.createElement('mj-raw', {
    dangerouslySetInnerHTML: {
      __html: html
    }
  });
}

<Mjml>
  <MjmlBody>
    <Raw html={`{{now format="YYYY"}}`} />
  </MjmlBody>
</Mjml>

It worked for me. 🤔

@kdallmeyer-sr
Copy link
Author

Works! Although I wish it was built in so a hack like this isn't necessary.

Thanks!

@kprokopczyk
Copy link

kprokopczyk commented Oct 21, 2020

@mastertheblaster the above solution works as far as preventing escaping, but i realized the rendered html retains the <mj-raw> tags.
So this:

<MjmlText> {`Copyright `} <RawHtml html={"{{now format='YYYY'}}"} /></MjmlText>

becomes this mjml: <mj-text>Copyright <mj-raw>{{now format='YYYY'}}</mj-raw>
becomes this html:<div>Copyright <mj-raw>{{now format='YYYY'}}</mj-raw></div>

Do you see the same thing on your end?

@mastertheblaster
Copy link
Collaborator

Hey @kprokopczyk,

The issue you describe is related to mjml engine itself. Seems like it doesn't allow mj-raw tags inside the mj-text.
Try yourself in their online tool https://mjml.io/try-it-live

In your usecase i would try a little bit different approach:

function Raw({ tag, html }) {
  return React.createElement(tag || 'mj-raw', {
    dangerouslySetInnerHTML: {
      __html: html
    }
  });
}

<Mjml>
  <MjmlBody>
    <Raw html={`{{now format="YYYY"}}`} />
    <MjmlText>
      <Raw tag="span" html={`{{now format="YYYY"}}`} />
    </MjmlText>
  </MjmlBody>
</Mjml>

@mastertheblaster mastertheblaster added the enhancement New feature or request label Oct 21, 2020
@mastertheblaster
Copy link
Collaborator

I have added the MjmlHtml component as an extension as it's not the Mjml* element. The usage looks like:

import { MjmlHtml } from 'mjml-react/extensions';

<MjmlHtml tag="span" html={`{{now format="YYYY"}}`} />

@felixmosh
Copy link

felixmosh commented Jan 12, 2021

Weird question, @kdallmeyer-sr why would you pass to something like handlebars?
The whole point of mjml-react is to allow you to generate "templates" using React.
In your case I would use a format function inside react.

import format from 'date-fns';

<MjmlText>{format(new Date(), 'YYYY')}</MjmlText>

@kdallmeyer-sr
Copy link
Author

Its a good question, not a weird one!

The ESP we use uses handlebars to fill in user properties. We use MJML as a template to generate an html+handlebars template for the ESP. Previously we had to use their WYSIWYG to generate templates or handcode. Using MJML allowed us a third option that makes it much easier for engineers.

Hoped that answered your question.

@tsah-balance
Copy link

tsah-balance commented Jul 4, 2022

Hi - bumping this - as I could not find anywhere else that addresses this issue.
What can be the solution if I would like to generate the html with handlebars {{#each}}, for example:

<MjmlTable color="#777777">
        <tr>
          <th>ID</th>
          <th>Name</th>
        </tr>
  {{#each patients}}
        <tr>
          <td> </td>
          <td>2</td>
        </tr>
  {{else}}
        <tr>
          <td>NO DATA</td>
        </tr>
  {{/each}}
 </MjmlTable>

@daliusd
Copy link
Contributor

daliusd commented Jul 4, 2022

Hi - bumping this - as I could not find anywhere else that addresses this issue. What can be the solution if I would like to generate the html with handlebars {{#each}}, for example

It is react library - use react for this. Otherwise you have two options:

  1. Use mjml directly. Potentially you don't need mjml-react.
  2. Use handlebars to generate js/ts files with mjml-react

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants