nmcteam / mulch

A frontend gulpfile boilerplate that uses Twig.js, LESS and/or SASS compilation and BrowserSync. Perfect for PHP CMS projects.
MIT License
15 stars 5 forks source link

Filter out partials when compile #9

Open jvandenrym opened 8 years ago

jvandenrym commented 8 years ago

I noticed that templates are compiled as well ( _default.html, _header.html) when running gulp mulch-compile where it supposed to only compile the pages themselves.

Suppose before compilation could filter them out?

JoelSutherland commented 8 years ago

Good catch, I will investigate this

JoelSutherland commented 8 years ago

What is the structure of your templates folder? Are you putting the partials in _urls? I put them in sibling folders.

jvandenrym commented 8 years ago

Fixed it by adding gulp filter plugin and filtering out _.*html files. I put all pages (and subdirs with pages), includes and templates in src/templates hence the filter.

gulp.task('twig',function(){
    const twigPartialsFilter = filter(file => !/\/_/.test(file.path));
    return gulp.src('templates/**/*.html')
        .pipe(plumber({
          errorHandler: function (error) {
            console.log(error.message);
            this.emit('end');
        }}))
        .pipe(data(getJsonData))
        .pipe(foreach(function(stream,file){
            return stream
                .pipe(twig())
        }))
        .pipe(twigPartialsFilter)
        .pipe(gulp.dest('../dist/'));
});
olets commented 8 years ago

@jvandenrym I'm not sure that mulch is supposed to skip over partials when compiling. For example, if you're using mulch to develop a site locally before moving it onto a CMS, you'll need those compiled partials.

But I like your idea of being able to exclude certain files. The simplest way to do this is by changing the globbing pattern in the twig gulp task – instead of selecting all files, select all files that do not start with your target character and that are not in a folder that start with your target character.

I've added this in https://github.com/nmcteam/mulch/pull/11. Instead of using an underscore, I'm excluding files and directories that start with hyphen (-)

olets commented 8 years ago

@jvandenrym don't know if you saw it, but @JoelSutherland said here that the main mulch shouldn't enforce conventions like "files named like this are excluded." Makes sense to me - that way it remains flexible enough for any sort of project.

But sorry, I misunderstood your point before and you're right: everything in /src/templates needs to be run through twig() even if it doesn't get passed into /compiled. Would const twigCompileFilter = filter('src/templates/urls/**/*'); do it?