Boostport / mjml-go

Compile MJML to HTML directly in your Go applications!
Apache License 2.0
84 stars 6 forks source link

How to escape template control structures in MJML to allow them to pass through to html? #20

Closed geoff-maddock closed 6 months ago

geoff-maddock commented 6 months ago

I'm using mjml-go to process an mjml file into HTML and then html/template to do variable replacement. In the mjml file, I'd like to be able to use control structures like if and range.

When I pass an mjml file that contains the code below into a call like:

htmlData, err = mjml.ToHTML(context.Background(), string(data), mjml.WithMinify(true))

The HTML does not contain the if and range templates, but it does contain the {{.NoteAnything}} variable replacements.

Is there a way to allow these structures to pass through?

    {{if .PreviousNotes}}
      {{range .PreviousNotes}}
        <mj-section background-color="#ffffff" padding="12px 0">
          <mj-column>
            <mj-table border="1px solid #bbbbbb" padding="0 24px" font-family="Inter">
              <tr style="border-bottom: 1px solid #bbbbbb;">
                <td style="padding: 8px 12px; font-size: 12px;"><span style="font-weight: 600">{{.NoteUserName}}</span>: {{.NoteUserTitle}} - {{.TicketClientName}}</td>
                <td style="padding: 8px 12px; text-align: right; font-size: 12px; font-weight: 600;">{{.NoteCreatedAt}}</td>
              </tr>
              <tr>
                <td colspan="2" style="padding: 8px 12px">
                  {{.NoteBody}}
                </td>
              </tr>
              <tr>
                <td colspan="2" style="padding: 8px 12px; font-size: 12px;">
                  <span style="font-weight: 600">Visibility:</span> {{.NoteVisibility}}
                </td>
              </tr>
            </mj-table>
          </mj-column>
        </mj-section>
      {{end}}
    {{end}}
geoff-maddock commented 6 months ago

Looks like using the mj-raw tag worked. Closing.

<mj-raw>
    {{if .PreviousNotes}}
      {{range $element := .PreviousNotes}}
      </mj-raw>
   ...