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>;">
<h1>My Heading</h1>
<div style="color:<div data-marker=" class="stylingblock-content-wrapper">#A569BD</div>;">
<p>My body</p>
<div style="color:<div data-marker=" class="stylingblock-content-wrapper">#F1C40F</div>;">
<p>My footer</p>
<div style="color:<div data-marker=" class="stylingblock-content-wrapper">#AAB7B8</div>;">
<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?