The ContentBlockByName() function has five parameters:
contentBlockName (string): Required. The full path of the content block to retrieve.
impressionRegionName (string): The name of the impression region to associate with the content block.
boolErrorOnMissingContentBlock (boolean): If true, the function returns an error if the content block can’t be found. If false, the function doesn’t return an error. The default value is true.
errorMessage (string): The content to return if an error occurs while retrieving the content block.
statusCode (number): The exit code of the function. A value of 0 indicates the function found the content block and successfully rendered the content. A value of -1 indicates either no content or an invalid content block.
Usage
To call the function, provide the name of the content block that you want to return.
%%[
Set @ContentBlockName = "Content Builder\Weekly Portfolio"
]%%
<p>%%=ContentBlockByName(@ContentBlockName)=%%</p>
If the function finds the content block, it returns the content in it. Otherwise, it returns an error message.
Adding an Impression Region
You can optionally begin an impression region when you insert the content block. This example inserts the content block with the name "Content Builder\Weekly Portfolio" and creates an impression area called "bodytext" at the same time.
%%[
Set @ContentBlockName = "Content Builder\Weekly Portfolio"
]%%
<p>%%=ContentBlockByName(@ContentBlockName, "bodytext")=%%</p>
Use the EndImpressionRegion() function to denote the end of the impression region.
Using the Status Code
You can use the status code parameter to show fallback content if the function wasn’t able to retrieve the content block.
%%[
Var @Content, @ContentBlockName, @StatusCode
Set @ContentBlockName = "Content Builder\Weekly Portfolio"
Set @Content = ContentBlockByName(@ContentBlockName, "bodytext", false, "We're sorry, but something went wrong... ", @StatusCode)
Output(@Content)
If @StatusCode == -1 then
Output(Concat('Visit the <a href="https://www.example.com/help">Example.com Help Pages</a> for more information.'))
EndIf
]%%
If the specified content block is found, the function outputs the content of the content block. Otherwise, the function outputs the fallback message and the message shown in the If statement.