grunt icon indicating copy to clipboard operation
grunt copied to clipboard

Is there a way to keep Template Strings From Being Processed?

Open Izzmo opened this issue 9 years ago • 6 comments

Simple question: is it possible to keep template strings from getting processed?

I shouldn't have to do this:

replacements: [{
   from: 'something',
   to: '<script src="<ConfigurationManager.AppSettings("something")>"></script>'
},{
   from: 'ConfigurationManager.AppSettings("something")',
   to: '%=ConfigurationManager.AppSettings("something")%'
}]

Izzmo avatar Feb 13 '16 03:02 Izzmo

The documentation for lodash indicates that \\ can be used to escape the open and close angle brackets.

https://lodash.com/docs#template

so for example:

replacements: [{
   from: 'something',
   to: '<script src="\\<%=ConfigurationManager.AppSettings("something")%\\>"></script>'
}]

nycdotnet avatar May 30 '16 15:05 nycdotnet

@nycdotnet the problem is that those output like:

<script src="\<%=ConfigurationManager.AppSettings("something")%\>"></script>

instead of

<script src="<%=ConfigurationManager.AppSettings("something")%>"></script>

Not sure why it's keeping the extra slash in there.

Izzmo avatar Jun 27 '16 19:06 Izzmo

It's most likely upstream issue. Did you try to test it with native lodash _.template(...)? Does it produce those slashes too?

ArmorDarks avatar Aug 05 '16 19:08 ArmorDarks

@ArmorDarks Sort of.

This adds a slash (making it a single backslash outputs fine though..):

let x = _.template('test <%= test %>!');
x({ test: '<script src="\\<%=ConfigurationManager.AppSettings("something")%\\>"></script>' });

// output: "test <script src="\<%=ConfigurationManager.AppSettings("something")%\>"></script>!"

Izzmo avatar Aug 05 '16 20:08 Izzmo

Oh, now I see why it doesn't work.

Here is example from docs:

// Use backslashes to treat delimiters as plain text.
var compiled = _.template('<%= "\\<%- value %\\>" %>');
compiled({ 'value': 'ignored' });
// ➜ '<%- value %>'

Please, note that you should wrap escaped template into template tags too to make it work.

_.template('\\<%- value %\\>')()
\\ returns `\<%- "value" %\>`

_.template('<%= "\\<%- value %\\>" %>')()
\\ returns `<%- value %>`

Though, what is really strange, while '<%= "\\<%- value %\\>" %>' works in lodash templates, Grunt for some reasons completely ignores escaping and trying to process whole template, which results in Warning: An error occurred while processing a template (value is not defined).

So, in the end of all, it's Grunt's issue.

ArmorDarks avatar Aug 05 '16 20:08 ArmorDarks

@ArmorDarks thanks for writing that out, that helps. So, now, where in the Grunt code should this be fixed, do you think?

Izzmo avatar Aug 05 '16 22:08 Izzmo