Runtime verified Test scripts included

Syntax

TreatAsContent(stringToReturn)  →  string
1 argument — exactly

Parameters

Name Type Required Description
stringToReturn string Yes String whose embedded AMPscript is evaluated

Example

%%[
  VAR @out
  SET @out = TreatAsContent("pre-%%=Add(2,3)=%%-post")
]%%
Result: %%=v(@out)=%%

Renders Result: pre-5-post.

A common use is to evaluate content that was assembled or fetched at runtime — a data extension field, a looked-up block — so that any AMPscript it carries runs instead of being shown literally:

%%[
  VAR @body
  SET @body = Field(@row, "Content")
]%%
%%=TreatAsContent(@body)=%%

Return value

string — the input with its embedded AMPscript evaluated and substituted in place.

There is no closed set of sentinel values: the result is whatever the rendered string happens to be.

Behaviour

Embedded AMPscript is executed, not escaped. TreatAsContent("pre-%%=Add(2,3)=%%-post") renders pre-5-post — the inline expression runs and its result replaces it, while the surrounding literal text is preserved unchanged.

Plain text passes through unchanged. A string carrying no AMPscript, such as just plain text, is returned exactly as given.

The empty string is valid and returns empty. TreatAsContent("") renders the empty string with length zero, rather than aborting or returning a placeholder.

The value is returned inline. The result can be assigned to a variable and reused, not only written straight to the page.

Show test script
%%[
  VAR @b, @tacEval, @tacPlain, @tacEmpty
  SET @b = RequestParameter("b")

  /* safe sweep: every case is accepted and can share one request */
  IF @b == "safe" THEN
    SET @tacEval = TreatAsContent("pre-%%=Add(2,3)=%%-post")
    OutputLine(Concat("tacEval=[", @tacEval, "]"))
    SET @tacPlain = TreatAsContent("just plain text")
    OutputLine(Concat("tacPlain=[", @tacPlain, "]"))
    SET @tacEmpty = TreatAsContent("")
    OutputLine(Concat("tacEmpty=[", @tacEmpty, "] LEN=[", Length(@tacEmpty), "]"))
  ENDIF
]%%

Because the embedded content is executed, never pass unreviewed input straight into TreatAsContent: a value spliced into the string is evaluated as AMPscript. Sanitise or allow-list any user-supplied content first.

Availability

Platform Available
Marketing Cloud Engagement Yes
Marketing Cloud Next No

See also