Is there a template engine that will parse templates in the style of ES6 template literals (e.g. "string ${var}"
) without violating a Content-Security-Policy (CSP) restriction on script evaluation?
The CSP restrictions on script evaluation prevent eval
, new Function
, setTimeout(string)
and setInterval(string)
.
There are a number of template engines that can provide or be modified to provide something like ES6-style template literals, such as John Resig's MicroTemplates, lodash _.template and DoT.js. However all seem to violate the CSP by using new Function
.
It would in some ways be convenient if var
could be unrestricted Javascript, but for apparent reasons this may not be possible. However I would need to be able to modify the engine to format the output as desired.
In the circumstances performance is not a concern, and pre-compiling the templates is not an option. Others have discussed pre-compilation.
As an additional restriction, the content is text - not HTML. I do not think DOM-oriented templating engines such as Knockout or PURE would not work effectively, as a result.
My first thought is to start with mustache.js and modify it from there (i.e. change mustache.tags = ['${', '}']
or a DIY solution, but I would be grateful for any thoughts on the topic in general as there seems to be quite a dearth of discussion on CSP and templates.