m_kotowski
New Creator

Render HTML of CMS_INPUT_DOM with links as JSON value

In the context of CaaS and JSON, if you want to render HTML of a CMS_INPUT_DOM that contain links and store it as JSON value, you might have encountered that the resulting value is not what you have expected:

{"text":"<p>Text <a href=\"#\" data-link-template=\"test_link\" data-link-id=\"48412828-926f-4b3c-ad4e-6823b32824c4\">Link<\/a> Text<\/p>"}

The JSON channel automatically adds data-link-template and data-link-id as well as a dummy href value but ignores your HTML link template completely.

To solve this issue, you can, for example, create a render template (ft_html_creator_for_cms_input_dom) with the following code in the JSON channel:

$--

      Build HTML for a CMS_INPUT_DOM element.

      Hint: Using the CaaS channel does not provide valid outputs for link templates, neither with CMS_VALUE, nor with toText(true).

     Parameters:

      * ft_cmsInputDom specifies the CMS_INPUT_DOM component

     * ft_result specifies a map that gets the HTML result with the key "html"

--$

$CMS_SET(set_oldTemplateSet, #global.templateSet)$

$CMS_SET(#global.templateSet, #global.project.templateSets[0])$

$CMS_SET(set_renderedHTML)$$CMS_VALUE(ft_cmsInputDom)$$CMS_END_SET$

$CMS_SET(void, ft_result.put("html", set_renderedHTML.toString()))$

$CMS_SET(#global.templateSet, set_oldTemplateSet)$

The caller, for example, a section template uses the following code snippet in its JSON channel:

$CMS_SET(set_result, {:})$$-- ft_html_creator_for_cms_input_dom adds "html" key --$

$CMS_RENDER(template: "ft_html_creator_for_cms_input_dom", ft_cmsInputDom: st_text, ft_result: set_result)$

$-- The resulting HTML snippet of the CMS_INPUT_DOM is stored in: set_result.get("html") --$

Labels (2)