NightlyCommit / twing

First-class Twig engine for Node.js
BSD 2-Clause "Simplified" License
199 stars 23 forks source link

[Question] Can twing be used in a gulp-like workflow? #20

Closed zomars closed 6 years ago

zomars commented 6 years ago

Can you provide an example? I actually use Nunjucks and it gets the job done, but, I would like to keep the syntax the most twig-like as possible. The reason being that I like to code my templates in a static manner first and then turning them into a theme for a CMS like Bolt or Wordpress.

ericmorand commented 6 years ago

There is no gulp plugin yet, but gulp workflow being based on piping streams into each other, it's trivial to have Twing included in that type of workflow. From the top of my head, something like this should work:

var through = require('through2');

let Twing = require('twing');
let loader = new Twing.TwingLoaderFilesystem('.');

let gulpPlugin = function(file) {
    let s = through(function(file, encoding, callback) {
        let twing = new Twing.TwingEnvironment(loader);

        twing.render(file.toString()).then(
            function(data) {
                callback(null, data);
            }
        );
    });

    s.write(file);

    return s;
};

let plugin = gulpPlugin('index.html.twig');

plugin.pipe(process.stdout);

That should make a nice starting point. What do you think?

zomars commented 6 years ago

Interesting I'll take a dive and see what can I accomplish.

ericmorand commented 6 years ago

Good! Let me know how it goes. I close the issue for now but feel free to comment.

zomars commented 6 years ago

I've got it working so far. I'm getting some error but I'm guessing is because of the coverage not being 100% yet:

TypeError: extension.getFilters is not a function

I will keep tinkering with it to see how far I can go.

ericmorand commented 6 years ago

Hmmm. That's not normal. Can you give me a test case for that error so that I look at it?

olets commented 6 years ago

This is exciting. I have a bunch of projects on gulp-twig, looking forward to dropping in Twing and trying it out.

ericmorand commented 6 years ago

@zomars, @olets, we now have an official gulp plugin:

https://github.com/ericmorand/gulp-twing

zomars commented 6 years ago

olets commented 6 years ago

@ericmorand sweeeet

I recommend bumping Twing to at least v1 (I'd just add 1 and do 1.9.0). npm semver matching doesn't kick in until v1, so gulp-twing's current "twing": "^0.8.1" is effectively "twing": "0.8.1"

ericmorand commented 6 years ago

@olets, you're right but I'm not comfortable bumping to v1.0+ before everything is fully tested and documented. And Twing is a peer dependency so it's actually ok.

V1.0 is just 3-4 weeks away if everything keep on going smooth.

olets commented 6 years ago

Ah yes indeed I missed the peer dependency aspect. Looking forward to trying it out!