Chris McKeown
Chris McKeown

Reputation: 1

Dynamically render page type property as HTML in Kentico macro transformation

I'm using the Kentico CMS application to build and render website pages. I've defined a page type and a transformation for the page type. Currently the transformation has it's Transformation type set to "Text / XML". On the page type I've defined a property of data type "Long text" and am using a "Rich text editor" for the Form control. The content of this field (HTMLContent) is expected to be a valid HTML element. And I'm attempting to render the HTML using "HTMLEncode(HTMLContent)".

However, when I dynamically render the string as HTML in my Macro it results in a string containing the HTML element and not the HTML element itself. i.e: "Some Content" instead of Some Content.

Is it possible to accomplish this dynamic rendering of HTML in a Kentico Macro?

Relevant Source:

{%
    return 
    "<div>" +
         HTMLEncode(HTMLContent) +
    "</div>";
%}

Upvotes: 0

Views: 686

Answers (1)

Michael419
Michael419

Reputation: 101

You are seeing the HTML printed on the web page because you are using the HTMLEncode() method - you don't need to use this method for what you are trying to achieve.

Just reference the page type field directly in the macro and the HTML generated in the rich text editor will be rendered. Assuming the name of the page type field is "HTMLContent", enter this into your transformation:

<div>
  {% HTMLContent %}
</div>

Relevant Kentico documentation can be found below:

https://docs.kentico.com/k10/developing-websites/loading-and-displaying-data-on-websites/writing-transformations/using-transformations-in-macro-expressions#Usingtransformationsinmacroexpressions-Displayingpagesfromthecontenttree

Upvotes: 2

Related Questions