patrickkettner / grunt-compile-handlebars

grunt plugin to compile static html from a handlebars plugin
MIT License
115 stars 29 forks source link

File contents not overwriting existing contents #56

Closed iamdriz closed 8 years ago

iamdriz commented 9 years ago

So I have the following template:

{{> _header}}
CONTENT!
{{> _footer}}

And then I have the following grunt task

module.exports = function(grunt) {
  'use strict';

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        'compile-handlebars': {
            allStatic: {
                files: [{
                    src: 'src/index.handlebars',
                    dest: 'dist/index.html'
                }],
                partials: ['src/partials/_*.handlebars'],
                templateData: ''
            }
        }
    });

    grunt.loadNpmTasks('grunt-compile-handlebars');

    grunt.registerTask('default', ['compile-handlebars']);

};

However everytime I run it then index.html just gets the content duplicated rather than replaced.

So for example after running the task 3 times one after the other, the index contents are:

<div class="header"></div>CONTENT!
<div class="footer"></div><div class="header"></div>CONTENT!
<div class="footer"></div><div class="header"></div>CONTENT!
<div class="footer"></div>

How do I stop this? It should replace the contents rather than append to it.

Also how come the footer is on a new line, but the content ends up on the same line as the header?

awolfe76 commented 9 years ago

:+1: Is there a way to have it wipe the file before adding content? Or do you recommend using another grunt plugin prior to compiling to clear the destination?

Great plugin by the way. Have had this use case for a while now and finally found this!

iamdriz commented 9 years ago

I'd say wiping the file should be default behaviour and maybe an option to turn it off if someone needs it (although I can't think of a scenario where you would want to do that)

To me, this plugin is no different to compiling CSS or JS and it always wipes the file first before adding the newly compiled code.

Thoughts?

Sent from my iPhone

On 11 Jun 2015, at 19:10, Andrew Wolfe notifications@github.com wrote:

Is there a way to have it wipe the file before adding content? Or do you recommend using another grunt plugin prior to compiling to clear the destination?

— Reply to this email directly or view it on GitHub.

awolfe76 commented 9 years ago

Agree! I can't think of a reason why you would want to append the output, which is why I was curious about the recommendation of another plugin to wipe it first.

FWIW I've started using https://github.com/gruntjs/grunt-contrib-clean to handle that for me.

Fertogo commented 9 years ago

I agree. For now, you can just change line 229 and set appendToFile to false in tasks/compile-handlebars.js

patrickkettner commented 8 years ago

This is the behavior because you may have multiple files that are being appended to during the run. In addition,

If you want to remove files between runs, I suggest grunt-contrib-clean