MDL-52136 core: Add support for quoting variables in mustache helpers
commit0b4bff8ca900695cd6c8b85c5d094de190762450
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 12 Feb 2016 04:42:41 +0000 (12 12:42 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 2 Mar 2016 00:48:46 +0000 (2 08:48 +0800)
tree4c798de11bbdf95f8778f6ffbdb1595fc61aa632
parent03b8b55f101813b292659db9a65f7b29b77947f4
MDL-52136 core: Add support for quoting variables in mustache helpers

This is required for when helpers include json-encoded variables as arguments.
As an example, imagine a template with content:

{{# str }} somekey, someidentifier, { "fullname": "{{ fullname }}" } {{/ str }}

If the fullname variable were to include the double-quote character (e.g.
John "Trevor" Doe) because of the way in which mustache renders content, it
would become:

{{# str }} somekey, someidentifier, { "fullname": "John "Trevor" Doe" } {{/ str }}

This results in an invalid JSON structure.

To work around this issue, the quote characters in the passed variable
must be escaped:

{{# str }} somekey, someidentifier, { "fullname": "John \"Trevor\" Doe" } {{/ str }}

Unfortunately, Mustache provides no way of doing so natively.

With this function, we can quote the text as appropriate:

{{# str }} somekey, someidentifier, { "fullname": {{# quote }}{{ fullname }}{{/ quote }} } {{/ str }}

This also handles the case where the quoted content includes the Mustache
delimeter ({{ or }}).

For example:
fullname = 'John "}}Trevor{{" Doe'

Ordinarily this would be rendered as:
{{# str }} somekey, someidentifier, { "fullname": "John "}}Trevor{{" Doe" } {{/ str }}

This rendering is both a JSON error, and also a mustache syntax error because of the mustache delimeters.

The quote helper also escapes these by wrapping them in change delimeter
tags:

{{# str }} somekey, someidentifier, { "fullname": "John "{{=<% %>=}}}}<%={{ }}=%>Trevor{{=<% %>=}}{{{{=<% %>=}}" Doe" } {{/ str }}
lib/amd/src/templates.js
lib/classes/output/mustache_quote_helper.php [new file with mode: 0644]
lib/classes/output/mustache_string_helper.php
lib/outputrenderers.php