使用Grunt进行基本包含



我有一个应用程序,它有3个页面,我希望这些页面是自包含的。为了DRY代码的利益,我想对页眉和页脚内容进行"includes"。我已经看过grunt-html构建的文档和示例,但不知怎么的,我还是功亏一篑。所有HTML都在路径'src/html'中,includes位于名为"includes"的子文件夹中:'src/html/includes'

以下是HTML示例:

<!doctype html>
<html>
  <head>
  <!-- build:section head --><!-- /build -->
  </head>
  <body>
  <p>A bunch of unique content for each of the pages</p>
  <!-- build:section footer --><!-- /build -->
  </body>
</html>

然后在我的咕哝文件中,我有以下内容:

htmlbuild: {
  src: 'src/html/app-*.html',
  dest: 'dist/',
  options: {
    sections: {
      head: 'src/html/includes/head.html',
      footer: 'src/html/includes/footer.html'
    }
  }
}

我确信这只是语法问题,但我似乎无法克服这个错误:

Warning: an error occurred while processing a template (Unexpected identifier).

它是一个"意外标识符"这一事实告诉我没有正确地点出"I"或叉过"t"。请欣赏更有经验的眼睛!

注意:我曾考虑过使用grunt contrib concat,但如果不使用globbing,我将不得不制作3个单独的任务来保持唯一内容的完整性。


[编辑添加:]

我成功地完成了我的基本用例,使用了一个不同的grunt任务,称为(适当地(grunt includes。我能够适当地包含我的文件。

然而,我仍然对grunt-html构建的能力感兴趣,它可以有条件地构建开发包或分发包。任何见解都值得赞赏!

htmlbuild是一个多任务,因此您需要在目标下定义文件:

module.exports = function(grunt) {
    grunt.initConfig({
        htmlbuild: {
            dist: {
                src: 'src/html/app-*.html',
                dest: 'dist/',
                options: {
                    sections: {
                        head: 'src/html/includes/head.html',
                        footer: 'src/html/includes/footer.html'
                    }
                }
            }
        }
    });
    grunt.loadNpmTasks('grunt-html-build');
    grunt.registerTask('default', ['htmlbuild']);
};

最新更新