How to prevent the Free Form from formatting AMPscript

How to prevent the Free Form from formatting AMPscript

Have you ever tried to insert AMPscript into a Free Form Content Block? Then you know for sure that they don’t like each other very much!

As a matter of fact, the WYSIWYG behind the Free Form formats the HTML every time we open the Free Form or when we click on the Content tab.

Problem

The formatting rules are strict and therefore every AMPscript line is seen as a simple line of text. The problem occur when we put our AMPscript inside an HTML tag that only accepts a particular tag as a child.

For example, the <ul> tag only accept <li> tag as a child and therefore the following code:

<ul>
  <li>Item 2</li>
  %%[ IF @v == 1 THEN ]%%
  <li>Item 2</li>
  %%[ ENDIF ]%%
</ul>

Will result in this mess after closing and opening the Free Form or clicking on the Content tab:

<ul>
  <li>
    Item 2
  </li>
  <li>
    %%[ IF @v == 1 THEN ]%%</li>
  <li>
    Item 2
  </li>
  <li>
    %%[ ENDIF ]%%</li>
</ul>

Solution

The solution is pretty simple! Just wrap your AMPscript code blocks in the HTML comment tags. In this way, the formatting will be ignored and the AMPscript will be executed properly.

<ul>
  <li>Item 2</li>
  <!--%%[ IF @v == 1 THEN ]%%-->
  <li>Item 2</li>
  <!--%%[ ENDIF ]%%-->
</ul>

Have I missed anything?

Please poke me with a sharp comment below or use the contact form.

Pay me a coffee

Want to say thanks? Pay me a coffee! Remember, I turn coffee into code.

  1. Thank you, I didn’t think of that and so far we didn’t receive any complaints… I’m going to put this to the test asap

  2. Adam,

    I put my AMPscript in comments in a Free Form component and looked at the text preview.

    Everything is fine.

    Could you provide me with an example of where it could go wrong?

Leave a Reply

Your email address will not be published. Required fields are marked *

server-side Javascript
Up Next:

How to create and move to folders with server-side Javascript

How to create and move to folders with server-side Javascript