Can Grunt Lodash Templates Be Used In Gruntfile.js tasks?

安稳与你 提交于 2019-12-24 08:16:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!