Grunt Lodash模板可以在Gruntfile.js任务中使用吗? [英] Can Grunt Lodash Templates Be Used In Gruntfile.js tasks?

查看:93
本文介绍了Grunt Lodash模板可以在Gruntfile.js任务中使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在gruntfile.js任务中使用grunt lodash模板,这样我就可以使用.toLowerCase()这样的函数。下面的代码片段不起作用,也许它不能以这种方式工作,因为我找不到适合此用例的示例。

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


解决方案

code>构造函数使用 toLowerCase

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

code> npm install 使用lodash:

  npm instal l 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,
转义:/<% - ([\\\\S] +?)%> / g
};

参考资料


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 %>',                                                     
        }]
    }
} 

解决方案

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 Lodash模板可以在Gruntfile.js任务中使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆