问题
Is there a way to use grunt lodash templating in gruntfile.js tasks so I can use functions like .toLowerCase(). The below snippet doesn't work, maybe it doesn't work this way since I can't find an example that fits this use case.
copy: {
plugins: {
files: [{
src: ['<%= root %>/<%= dirs.plugins %>/<%= pkg.name.toLowerCase() %>'],
dest: '<%= root %>/<%= dirs.www %>/wp-content/plugins/<%= pkg.name %>',
}]
}
}
回答1:
Use the evaluate delimiter <%
plus the String()
constructor to use toLowerCase
:
src: ['<% String(pkg.name).toLowerCase() %>']
Use npm install
to use lodash:
npm install lodash --save-dev
Then add the require
statement to your Gruntfile.js:
var _ = require('lodash');
var newArr = _.map(arr, fn);
By default, the template delimiters used by lodash are like those in embedded Ruby (ERB). Change the following template settings to use alternative delimiters.
_.templateSettings =
{
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
};
References
Grunt 0.4.2 released
ECMAScript: String.prototype.toLowerCase method
By default, Underscore uses ERB-style template delimiters
来源:https://stackoverflow.com/questions/24564053/can-grunt-lodash-templates-be-used-in-gruntfile-js-tasks