【问题标题】:pass middleman variable to grunt file将中间人变量传递给 grunt 文件
【发布时间】:2015-05-14 15:37:53
【问题描述】:

我的中间人模板有一个“id”变量,我将我的 html 电子邮件作业名称放入其中。

我知道如果我将中间人 erb 文件从 index.html.erb 更改为 newName.html.erb,它将作为最终文件名输出。

我的问题是我的大多数 grunt 任务都需要我希望它们运行的​​文件名(我尝试使用 *.html,但它只适用于某些任务)并且之前没有在 grunt 文件中编辑它如果我更改 erb 文件名,它们将不会执行。

有没有办法传递 grunt 的 'id' 变量来命名中间人正在输出的文件,并将该变量插入到各种任务中,以便他们也接受它作为文件名?

这是我的 grunt 配置:

grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        // Build html
        middleman: {
            options: {
                useBundle: true
            },
            server: {},
            build: {
                options: {
                    command: "build"
                }
            }
        },
        // Format html file
        prettify: {
            options: {
                // Task-specific options go here.
            },
            one: {
                src: 'build/index.html',
                dest: '_output/index.html'
            }
        },
        // Run the text converter
        execute: {
            simple_target_with_args: {
                options: {
                    // execute node with additional arguments
                    args: ['_output/index.html']
                },
                src: ['node_modules/node-text-converter/converter.js']
            }
        },
        'special-html': {
            compile: {
                files: {
                   '_output/index.html': '_output/index.html',
                }
            }
        },
        'phantomjs_screenshot': {
        main: {
            options: {
                delay: 1000
            },
            files: [{
                expand: true,
                cwd: '_output',
                src: ['**/*.html'],
                dest: '_output/screenshots/',
                ext: '.jpg'
            }]
        }
    }
});

【问题讨论】:

  • 您的设置描述有点模糊。 Grunt 任务实际上对构建的模板做了什么?可能有机会让中间人生成类似all_ids.erb.json 的内容并通过 Grunt 加载此文件。
  • 中间人构建文件后,prettify运行并将结果输出到新文件夹。然后是一个自定义构建的任务,它从 html 文件中提取所有文本以创建电子邮件纯文本版本(这是必须有一个文件名才能运行而不是在所有文件上运行的任务。接下来是一个任务编码 html 特殊字符,最后是截取电子邮件的任务。我将粘贴上面的 grunt 文件,以便您也可以实际看到它。

标签: javascript gruntjs html-email middleman


【解决方案1】:

您应该将变量存储在 JSON 文件中,并将其同时导入 Middleman 和 Grunt。

【讨论】:

    猜你喜欢
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    相关资源
    最近更新 更多