node.js - EJS 在 gruntfile 中转义

标签 node.js gruntjs

我的 gruntfile 中有以下代码:

'string-replace': {
  inline: {
    files: {
      'entity/templates/': 'entity/templates/*', 
    },
    options: {
      replacements: [{
        pattern: /Article/g,
        replacement: '<%= entityName %>'
      }, {
        pattern: /article/g,
        replacement: '<%= _.slugify(entityName) %>'
      }]
    }
  }
}

我正在尝试用表达式替换几个文件中的“文章”一词。我希望这个表达式在被替换的文件中如此打印,但现在它被评估了。

如何转义代码?

我正在使用 https://github.com/erickrdch/grunt-string-replace但我认为问题在于 grunt 如何评估表达式,对吗?

错误信息如下:

Warning: An error occurred while processing a template (entityName is not defined). Use --force to continue.

最佳答案

我现在通过这样做解决了它:

'string-replace': {
  inline: {
    files: {
      'entity/templates/': 'entity/templates/*', 
    },
    options: {
      replacements: [{
        pattern: /Article/g,
        replacement: '<%= entityName ENDTAG'
      }, {
        pattern: /article/g,
        replacement: '<%= _.slugify(entityName) ENDTAG'
      }, {
        pattern: /ENDTAG/g,
        replacement: '%>'
      }]
    }
  }
}

关于node.js - EJS 在 gruntfile 中转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21556642/

相关文章:

node.js - Express CORS 适用于除一个端点之外的其他端点

javascript - 将 JSON 文件导入运行在 Grunt 服务器上的 Angular 应用程序

javascript - 如果 Grunt 任务的子任务之一失败,如何使其失败?

javascript - 需要带有 Webpack 的 Modernizr

javascript - 如何将路由器/中间件添加到Features应用程序?

javascript - 如何从 spring data jpa 将值打印到用户界面?

Node.js/Mongojs 嵌套数据库回调返回

javascript - node.js 从 url 下载带有未知扩展名的图像

gruntjs - 为什么 grunt-contrib-watch livereload 不起作用?

javascript - Gruntjs 从自定义 json 文件读取设置