Print this page

Render dynamic content in AMPScript

Knowledge Article Number 000221186
Description
Find steps and examples for rendering your dynamic content in Marketing Cloud. 
Resolution


Render Dynamic Content


Follow these steps: 

1. Place the following code in a template email (This will allow use of the account-level headers and footers): 
 
%%[ 
Var @Content_A,@Content_B,@Content_C 
Set @Content_A = Test_A 
Set @Content_B = Test_B 
Set @Content_C = Test_C 
]%% 

%%[IF @Content_A == 'Yes' THEN]%% 
%%=ContentAreaByName("my contents\dynamic\Content_A")=%% 
%%[ELSEIF @Content_B == 'Yes' THEN]%% 
%%=ContentAreaByName("my contents\dynamic\Content_B")=%% 
%%[ELSEIF @Content_C == 'Yes' THEN]%% 
%%=ContentAreaByName("my contents\dynamic\Content_C")=%% 
%%[ELSE]%% 
No content to render.... this would be a subscriber who meets none of the previous criteria..this would be the default content if you wanted to have default content 
%%[ENDIF]%%
 

2. Create three ContentAreas (Content_A, Content_B and Content_C) in the 'My Contents' folder. Note that the full path to the ContentArea is specified in the ContentAreaByName() function, so "my contents\dynamic\Content_C" is Content_C under the 'Dynamic' subfolder under 'My Contents.' Keep the content simple in HTML (ie: <H1>This is content area A</H1>). This will allow you to test the function with basic content. 

3. Create a list and add subscribers who will have just the value for each condition in the code above. Once you have the email, the ContentAreas and the list, you should be able to send preview the email with the test list and see the dynamic content change as you preview each subscriber. 


Note: Based on the use of the IF THEN ELSEIF, the ContentArea rendered will be the first one to have a value of 'Yes' in the attribute. 
 


How the code works


The first section: 

 
%%[ 
Var @Content_A,@Content_B,@Content_C 
Set @Content_A = Test_A 
Set @Content_B = Test_B 
Set @Content_C = Test_C 
]%%

Creates the variables to contain the values of the attributes Test_A, Test_B and Test_C @CScore and then sets them with the corresponding attribute value. Set @Content_A = Test_A means set the variable @Content_A with the value of Test_A. 

Want to learn more? Read more about this type of AMPscript in our documentation
 


The main section:


This is an IF logic that looks at the value in the variable and then uses the ContentAreaByName function to insert a ContentArea. Currently, if the value falls outside the specified ranges then the information in the ELSE block will be inserted. This logic can be tweaked. 
 
%%[IF @Content_A == 'Yes' THEN]%% 
%%=ContentAreaByName("my contents\dynamic\Content_A")=%% 
%%[ELSEIF @Content_B == 'Yes' THEN]%% 
%%=ContentAreaByName("my contents\dynamic\Content_B")=%% 
%%[ELSEIF @Content_C == 'Yes' THEN]%% 
%%=ContentAreaByName("my contents\dynamic\Content_C")=%% 
%%[ELSE]%% 
No content to render.... this would be a subscriber who meets none of the previous criteria 
%%[ENDIF]%%


Want to learn more? Review more about the ContentAreaByName function (Scroll a little over half-way thru to find this topic). 




promote demote