【问题标题】:Can Grunt Lodash Templates Be Used In Gruntfile.js tasks?Grunt Lodash 模板可以在 Gruntfile.js 任务中使用吗?
【发布时间】:2014-07-03 22:13:38
【问题描述】:

有没有办法在 gruntfile.js 任务中使用 grunt lodash 模板,这样我就可以使用 .toLowerCase() 之类的函数。下面的 sn-p 不起作用,也许它不起作用,因为我找不到适合这个用例的示例。

copy: {                                                                             
    plugins: {
        files: [{                       
            src: ['<%= root %>/<%= dirs.plugins %>/<%= pkg.name.toLowerCase() %>'],
            dest: '<%= root %>/<%= dirs.www %>/wp-content/plugins/<%= pkg.name %>',                                                     
        }]
    }
} 

【问题讨论】:

    标签: javascript json gruntjs lodash


    【解决方案1】:

    使用评估分隔符&lt;% 加上String() 构造函数来使用toLowerCase

    src: ['<% String(pkg.name).toLowerCase() %>']
    

    使用npm install 来使用lodash:

    npm install lodash --save-dev
    

    然后将require 语句添加到您的 Gruntfile.js:

    var _ = require('lodash');
    var newArr = _.map(arr, fn);
    

    默认情况下,lodash 使用的模板分隔符类似于嵌入式 Ruby (ERB) 中的分隔符。更改以下模板设置以使用替代分隔符。

    _.templateSettings = 
      {
      evaluate    : /<%([\s\S]+?)%>/g,
      interpolate : /<%=([\s\S]+?)%>/g,
      escape      : /<%-([\s\S]+?)%>/g
      };
    

    参考文献

    【讨论】:

      猜你喜欢
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多