1

I would like to use AMPscript to dynamically set an inline style color, that is defined globally. Ultimately, the value will be returned from a Lookup function, but for now, here's a simplified version.

I have an HTML Content Block with the key color-test that looks like this:

%%[
var @color

if @colorName == 'head' then
   set @color = '#CD6155'
elseif @colorName == 'body' then
   set @color = '#A569BD'
elseif @colorName == 'footer' then
   set @color = '#F1C40F'
else
   set @color = '#AAB7B8'
endif
]%%%%=v(@color)=%%

And here is my email from Content Builder (which is a Paste HTML):

%%[ var @colorName ]%%
<!DOCTYPE html>
<html>
<head>
<title>email</title>
</head>
<body>
<div style="color:%%[ set @colorName='head' Output(ContentBlockByKey('color-test')) ]%%;">
   <h1>My Heading</h1>
</div>

<div style="color:%%[ set @colorName='body' Output(ContentBlockByKey('color-test')) ]%%;">
   <p>My body</p>
</div>

<div style="color:%%[ set @colorName='footer' Output(ContentBlockByKey('color-test')) ]%%;">
   <p>My footer</p>
</div>

<div style="color:%%[ set @colorName='subFooter' Output(ContentBlockByKey('color-test')) ]%%;">
   <p>My sub footer</p>
</div>

</body>
</html>

When the AMPscript is interpreted, the rendered HTML is broken:

On inspection, divs (with broken attributes) are placed around the interpreted Content Blocks:

<body>
<div style="color:<div data-marker=" class="stylingblock-content-wrapper">#CD6155</div>;"&gt;
   <h1>My Heading</h1>


<div style="color:<div data-marker=" class="stylingblock-content-wrapper">#A569BD</div>;"&gt;
   <p>My body</p>


<div style="color:<div data-marker=" class="stylingblock-content-wrapper">#F1C40F</div>;"&gt;
   <p>My footer</p>


<div style="color:<div data-marker=" class="stylingblock-content-wrapper">#AAB7B8</div>;"&gt;
   <p>My sub footer</p>

</body>

I'm aware that non-HTML Content Blocks are wrapped in divs, but I didn't think this applied to HTML Content Blocks.

I can't think of a way to avoid this, other than finding the divs and removing them, where the following HTML:

%%[ var @colorName ]%%

<!DOCTYPE html>
<html>
<head>
<title>email</title>
</head>
<body>
<div style="color:%%[ set @colorName='head' Output(RegExMatch(ContentBlockByKey('color-test'), '<div.*?>(.*)<\/div>', 1)) ]%%;">
   <h1>My Heading</h1>
</div>

<div style="color:%%[ set @colorName='body' Output(RegExMatch(ContentBlockByKey('color-test'), '<div.*?>(.*)<\/div>', 1)) ]%%;">
   <p>My body</p>
</div>

<div style="color:%%[ set @colorName='footer' Output(RegExMatch(ContentBlockByKey('color-test'), '<div.*?>(.*)<\/div>', 1)) ]%%;">
   <p>My footer</p>
</div>

<div style="color:%%[ set @colorName='subFooter' Output(RegExMatch(ContentBlockByKey('color-test'), '<div.*?>(.*)<\/div>', 1)) ]%%;">
   <p>My sub footer</p>
</div>

</body>
</html>

...produces the following HTML:

<body>
<div style="color:#CD6155;">
   <h1>My Heading</h1>

</div><div style="color:#A569BD;">
   <p>My body</p>

</div><div style="color:#F1C40F;">
   <p>My footer</p>

</div><div style="color:#AAB7B8;">
   <p>My sub footer</p>

</div>
</body>

and is displayed correctly...

While this works, it's somewhat of a hack. Also, it appears that I need to repeat the RegExMatch function each time and can't set it as a variable. For example, the following code:

%%[
var @colorName, @getValue
set @getValue = RegExMatch(ContentBlockByKey('color-test'), '<div.*?>(.*)<\/div>', 1)
]%%

<!DOCTYPE html>
<html>
<head>
<title>email</title>
</head>
<body>
<div style="color:%%[ set @colorName='head' ]%%%%=v(@getValue)=%%;">
   <h1>My Heading</h1>
</div>

</body>
</html>

...returns the error:

SET Statement Invalid<br>An error occurred when attempting to resolve a script expression. See inner exception for detail.
 Script Expression: RegExMatch(ContentBlockByKey('color-test'), '<div.*?>(.*)<!DOCTYPE html> <html> <head> <title>email</title> </head> <body> <div style="color:%%[ set @colorName='head'

I've tried different variations of setting and outputting variable and it still throws an error.

Does anyone has a better idea how I can approach this to avoid the inserted divs around my Content Block?

1 Answer 1

5

I think the output() function in your content block is causing the extra divs. I'd code the content block like this:

%%[

var @color

if @colorName == 'head' then
   set @color = '#CD6155'
elseif @colorName == 'body' then
   set @color = '#A569BD'
elseif @colorName == 'footer' then
   set @color = '#F1C40F'
else
   set @color = '#AAB7B8'
endif

]%%

And then in the email, just utilize the variables set in the block:

%%[ var @colorName ]%%

<title>email</title>

%%[ set @colorName='head' ContentBlockByKey('color-test') ]%%
<div style="color:%%=v(@color)=%%;">
   <h1>My Heading</h1>
</div>

%%[ set @colorName='body' ContentBlockByKey('color-test') ]%%
<div style="color:%%=v(@color)=%%;">
   <p>My body</p>
</div>

%%[ set @colorName='footer' ContentBlockByKey('color-test') ]%%
<div style="color:%%=v(@color)=%%;">
   <p>My footer</p>
</div>

%%[ set @colorName='subFooter' ContentBlockByKey('color-test') ]%%
<div style="color:%%=v(@color)=%%;">
   <p>My sub footer</p>
</div>

Output:

output

1
  • Thanks. I need to set the variable and output it in the same string, but you pointed me to the culprit; the Output function is creating the div. Here's the final implementation which also works: <div style="color:%%[ set @colorName='body' ContentBlockByKey('color-test') ]%%%%=v(@color)=%%;"> Sep 10, 2018 at 3:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .