Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt replace HTML with file contents

I have a production version of my html and I'm looking to compile my (individual) templates and place them into the prod version of the html file.

So far concat works great for building the single templates file, but I want to take code like the following:

<html>
    <head></head>
    <body>
        {{templates}}
    </body>
</html>

...and then replace {{templates}} with the source from the concat'd templates file. I'm finding a number of grunt plugins that do replace, but none of them seem to allow me to

like image 997
Fluidbyte Avatar asked Feb 13 '26 06:02

Fluidbyte


1 Answers

I ended up writing a multitask to handle it:

grunt.registerMultiTask('integrateTemplate', 'Integrates compiled templates into html file', function () {
    var data = this.data,
        path = require('path'),
        src = grunt.file.read(data.src),
        dest = grunt.template.process(data.dest),
        templates = grunt.file.read(data.compiledTemplate),
        out;

    out = src.replace(/\{\{templates\}\}/g, templates);

    grunt.file.write(dest, out);
    grunt.log.writeln('Template integrated');

});

And prod_template.html looks something like this:

<html>
    <head>
    </head>
    <body>
         {{templates}}
    </body>
</html>

Then I just call it with:

integrateTemplate: {
  main: {
    compiledTemplate: '<%= templates.main.dest %>', // Compiled template src
    src: 'prod_template.html',
    dest: 'index.html'
  }
}

Still, if there is a better suggestion (I'm sure there is), I would love to hear about it.

like image 160
Fluidbyte Avatar answered Feb 15 '26 20:02

Fluidbyte