olado/doT

Custom delimiters definition via helper function instead of regex

SalvaFiorenza8 opened this issue · 4 comments

Hi. I'm trying to change the default delimiters from this:

{{ }} for evaluation
{{= }} for interpolation
{{! }} for interpolation with encoding
{{# }} for compile-time evaluation/includes and partials
{{## #}} for compile-time defines
{{? }} for conditionals
{{~ }} for array iteration

To this:

<% %> for evaluation
<%= %> for interpolation
<%! %> for interpolation with encoding
<%# %> for compile-time evaluation/includes and partials
<%## #%> for compile-time defines
<%? %> for conditionals
<%~ %> for array iteration

But I'm struggling a little, cause I'm not an expert on regex and the examples/documentation are different from the actual code, I have done this changes so far:

doT.templateSettings = {
    evaluate: /\<\%([\s\S]+?(\}?)+)\%\>/g,
    interpolate: /\<\%=([\s\S]+?)\%\>/g,
    encode: /\<\%!([\s\S]+?)\%\>}/g,
    use: /\<\%#([\s\S]+?)\%\>/g,
    useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
    define: /\<\%##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\%\>/g,
    defineParams: /^\s*([\w$]+):([\s\S]+)/,
    conditional: /\<\%\?(\?)?\s*([\s\S]*?)\s*\%\>/g,
    iterate: /\<\%~\s*(?:\%\>|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\%\>)/g,
    varname: "it",
    strip: false,
    append: true,
    selfcontained: false,
    doNotSkipEncoded: false
};

But I'm not sure if these changes are enough, especially with the useParams and evaluate expressions, so I avoid using them in my templates, although I'll like to be able of safely use these features.

I was using this code to avoid hardcoding both regexps themselves and their list: https://github.com/epoberezkin/express-dot/blob/master/express-dot.js#L81

Probably worth adding to 2.0...

Thanks! I added the the helper to my project and had no issues so far. I made a pull request with the changes to branch v2.0.0

in v2