shannonmoeller / gulp-hb

A sane Gulp plugin to compile Handlebars templates. Useful as a static site generator.
http://npm.im/gulp-hb
MIT License
147 stars 14 forks source link

Will this plugin, can I do the following ? #27

Closed Avcajaraville closed 8 years ago

Avcajaraville commented 8 years ago

So, I have a node app.

I have some static pages, they only uses translations (i18n module), and nothing more.

There are no objects needed for this. The pages includes handlebars partials, layouts, etc (with .html extension).

The templates structure is something like this:

views |-> layouts (folder with layouts) |-> partials (folder with partials) |-> pag1.html |-> pag2.html

What I would like to do is a gulp plugin that generates those pages in all the languages we have.

The languages are in json files

This is a client request, who wants to serve the content as static pages.

Will this be possible with this plugin ?

shannonmoeller commented 8 years ago

You can do this, yes. You'd need to setup a gulp task that reads the language .json files then feed those into gulp-hb. Something like this:

var gulp = require('gulp');
var hb = require('gulp-hb');
var through = require('through2');

gulp.task('build', function() {
    return gulp
        .src('path/to/i18n/*.json')
        .pipe(through.obj(function(file, enc, done) {
            gulp
                .src('path/to/pages/*.html')
                .pipe(hb({
                    data: { i18n: JSON.parse(String(file.contents)) },
                    helpers: 'path/to/helpers/**/*.js',
                    partials: 'path/to/partials/**/*.hbs'
                }))
                .pipe(gulp.dest(path.join('path/to/output', file.stem)))
                .on('end', done);
        }));
});

This is untested code, so your milage may vary, but the principle is there. You start with your .json files, then one by one you build out separate copies of the site. I hope this helps!