1

We are using TinyMCE (6) as a WYSIWYG Editor so our users can enter in HTML content into Templates that we will use later. We are injecting certain variables, using the Mustache Templating Library, which are then replaced by our backend. We are finding that some Mustache variables are being re-written by TinyMCE when it is loaded into the editor, or when saved from the code plugin. How can we stop this from happening, and what is the ramifications?

An example is the following code block:

    <p>
    <strong>Scope of Services</strong>
    </p>
    <ul>
    {{#jobs}}
    <li>{{short_description}}</li>
    {{/jobs}}
    {{^jobs}}
    <li><em style="color: red;">NO SERVICES HAVE BEEN ADDED TO THIS ENGAGEMENT</em></li>
    {{/jobs}}
    </ul>

This will loop through a Mustache variable called Jobs, and create a line item in the UL for each job that exists.

But Mustache takes this code, and turns it into this:

    <p><strong>Scope of Services</strong></p>
    <ul>
    <li>{{#jobs}}</li>
    <li>{{short_description}}</li>
    <li>{{/jobs}} {{^jobs}}</li>
    <li><em style="color: red;">NO SERVICES HAVE BEEN ADDED TO THIS ENGAGEMENT</em></li>
    <li>{{/jobs}}</li>
    </ul>

Notice how it wrapped the mustache variables into LI elements.

How can we make it not do that, and ignore things that are in double or triple braces like {{variable}} and {{{variable}}}

Jepthy
  • 13
  • 4
  • Something has caused the delimiters of your Mustache tags to be invisible in your question. I realized this when I read the final sentence: "... ignore things that are in or {}". Could you please edit your question so that those delimiters are visible again? – Julian Aug 20 '23 at 09:24
  • @Julian, thanks for catching that! I have updated the code blocks. – Jepthy Aug 21 '23 at 13:43
  • Thanks for fixing that. One more question: could you clarify the workflow? Is it user input with HTML -> TinyMCE -> other frontend code that injects Mustache variables -> backend rendering? Or user input with HTML and Mustache variables -> TinyMCE -> backend? Or something else? – Julian Aug 21 '23 at 18:08
  • I am seeing the behavior when I load the data into TinyMCE via Javascript (i.e. it is saved in the database correctly, then we add the data to the form), when the user pasts the code into TinyMCE's Code window, etc. – Jepthy Aug 22 '23 at 00:00

1 Answers1

0

TinyMCE's primary focus is on editing rich text. For this reason, it tries hard to produce valid HTML, which is not supposed to contain Mustache tags. In particular, a <ul> can only contain text inside <li> subelements. Besides, the browser might be trying to normalize the HTML code as well.

My first guess would be like yours: attempt to circumvent this by using the Code plugin. Alas, that plugin is intended to give the user more control over the final HTML, but not to enable text entry in other languages such as Mustache.

I can think of three possible workarounds, but none of them seems ideal:

  1. Try your luck with another rich text editor. I have not used any of these myself, but according to Wikipedia, CKEditor might be a possible alternative. This solution does nothing to address the fact that the browser might be interfering as well, so it could be flaky.
  2. Switch to a less user-friendly plaintext editor. I know that a raw <textarea> will allow you to edit Mustache code, as I built the Wontache playground. However, I imagine this might be too crude.
  3. Switch to different delimiters for the Mustache tags, which HTML interprets as comments. I suggest <!--{ and }-->. In this way, you can still make regular HTML comments by leaving out the curly braces. Using delimiters that coincide with the target language is generally not ideal, but in this case, it might be the most workable solution.
Julian
  • 4,176
  • 19
  • 40