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.
Nice Trick & Tips ! Thanks
Not sure if it’s still an issue, but HTML comments used to cause problems with text-only versions of the email.
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
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?
I dont follow –
I thought
The code within the HTML comment tags in the code chunk is commented out – So .. In the context of HTML-based content blocks, anything between is treated as a comment and won’t be processed as code by the rendering engine or platform…..
(including AMPscript logic) i.e the logic won’t execute and it is as if the code doesn’t exist as far as the email rendering is concerned.
That would be restating the obvious .. but then
What am I missing?
Well, AMPscript is rendered in the back-end and the HTML comments are in the front-end. Back-end code is always executed first.