ContentBlockByKey
→ stringRenders a Content Builder asset by its customer key and returns the rendered HTML string.
Available in:
Email
CloudPage
Automation
Triggered Send
Syntax
ContentBlockByKey(customerKey)
1 argument
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
customerKey |
string | Yes | The customer key (external key) of the Content Builder asset. |
Description
ContentBlockByKey() renders a Content Builder block and returns the processed HTML. It’s the SSJS equivalent of the AMPscript ContentBlockByKey() function (the global function works identically to Platform.Function.ContentBlockByKey()).
The returned string contains the fully rendered HTML, including any personalization variables resolved within the content block.
Examples
Render a content block
var headerHtml = ContentBlockByKey("header-navigation");
Write(headerHtml);
Conditional content block
var sk = Platform.Request.GetQueryStringParameter("sk");
var isVIP = Platform.Function.Lookup("Subscribers", "IsVIP", "SubscriberKey", sk);
var blockKey = (isVIP === "1") ? "vip-welcome-block" : "standard-welcome-block";
Write(ContentBlockByKey(blockKey));
Composing a page from blocks
Write(ContentBlockByKey("page-header"));
Write('<main class="content">');
Write(ContentBlockByKey("main-content-" + pageId));
Write('</main>');
Write(ContentBlockByKey("page-footer"));
Notes
- The customer key is set in Content Builder under the asset’s properties
- The content block is processed server-side, so any AMPscript or SSJS inside it also executes
- Returns an empty string if the block is not found (no error thrown)
- Use
ContentBlockByName()if you don’t have the customer key