Introduction
I'm currently creating a templatebuilder where users can build a template for an app. The user can drag and drop multiple blocks, such as text blocks and 'custom code' blocks. The template will be parsed within an app. Right now, a template could look like this:
<section>
<div class="row">
<div class="col-sm-12">
<section data-type="code">
<#code></#code>
</section>
</div>
</div>
<div class="row">
<div class="col-sm-12" data-type="container-content">
<section data-type="text">
<u>Lorem</u> ipsum
</section>
</div>
</div>
</section>
So, this template contains two elements (see the data-type
attribute): one part is custom written code. Here, the user wrote custom code, including Apache Freemarker code. The second part is custom written text.
Situation
The code above will be used in two different ways.
- Exactly this code will be used inside an app that's using the template (so that's why they should be able to write Freemarker code, because this will be parsed).
- On my website, the user should be able to edit this template. Because the code is stored in the database as written above, there is a problem:
Problem
When I directly render the template in the web-interface, the text
part will render correctly with the <u></u>
tags, but the code
part will be rendered as html as well which might cause weird behaviour (such as the freemarker notation </#list>
being auto-converted to <!--#list-->
).
But, if I render the full template as text only, the text
part with the <u></u>
tags will not be rendered as well.
Expected outcome
I want to read the template variable with JavaScript / jQuery and then parse each data-type
with text
as html, and with code
as text.
How can I loop through the template and do this?