npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-twing

v4.0.0

Published

Twing plugin for gulp.js

Downloads

701

Readme

gulp-twing

NPM version Build Status Coverage percentage

Compile Twig templates with gulp. Build upon Twing.

Prerequisites

Installation

npm install twing gulp-twing --save

Usage

var twing = require('gulp-twing');

twing(env, data = {}, options = {})

Return an object transform stream that expects entry filenames.

  • env

    A TwingEnvironment instance.

  • data

    A hash of data passed to the render function of the template. See Twing documentation for details.

  • options

    An optional hash of options. The following options are supported:

    • outputExt

      The output file extension including the .. Defaults to .html.

    • templatePaths

      A path string or an array of path strings to pass to TwingLoaderFilesystem. Defaults to '.'.

Examples

The following examples all require importing gulp, gulp-twing and instantiating a Twing environment:

// top of gulpfile.js
var gulp = require('gulp');
var twing = require('gulp-twing');
var {TwingEnvironment, TwingLoaderRelativeFilesystem} = require('twing');

let env = new TwingEnvironment(new TwingLoaderRelativeFilesystem());

Basic usage

To compile all .twig files in the source directory src/ saving the output as .html in the destination directory dest/, use

// in gulpfile.js
function twig() {    
    return gulp.src('src/**/*.twig')
        .pipe(twing(env))
        .pipe(gulp.dest('dest'))
}

CLI usage

To expose the above function to the command line in gulp 3 or gulp 4, use gulp.task:

// in twig()
gulp.task('twig', twig);

Call the function with the command gulp twing.

If you don't need to support gulp 3, you can use the more terse gulp 4 syntax

// in twig()
gulp.task(twig);

Classic gulp syntax

It may be convenient to use the classic syntax, for example when migrating an older gulp project's Twig compilation to Twing. This is not recommended for new projects as it is not supported by gulp 4.

// in gulpfile.js
gulp.task('twig', function() {
    return gulp.src('src/**/*.twig')
        .pipe(twing(env))
        .pipe(gulp.dest('dest'))
});

Passing data

You can pass data to Twig templates during compilation with the gulp-twing's data hash.

For example, to compile the template

{# src/example.twig #}
{{ foo }}

to

<!-- dest/example.twig -->
bar

use

// in gulpfile.js
function twig() {
    return gulp.src('src/**/*.twig')
        .pipe(twing(env, {foo: 'bar'}))
        .pipe(gulp.dest('dest'))
}

Renaming files

By default, gulp-twing appends .html to compiled files. Changing this behavior is easy.

Strictly named templates

Use gulp-twing's outputExt option when applying the same file extension change to all source files. For example, use outputExt: '.ext' to compile example.twig as example.ext. Or use outputExt: '' to compile example.ext.twig as example.ext.

// in gulpfile.js

// src contains only *.css.twig and *.html.twig templates

function twig() {
    return gulp.src('src/**/*.twig')
        .pipe(twing(env, {}, {outputExt: ''}))
        .pipe(gulp.dest('dest'))
}

// dest will contain *.css and *.html files

Loosely named templates

If you need more control on the name of the ouput, use gulp-rename.

To compile index.css.twig, index.html.twig, and foo.twig saving the output to index.css, index.html, and foo.html, use

// in gulpfile.js
var rename = require('gulp-rename');

// src contains foo.twig, index.css.twig and index.html.twig

function twig() {
    gulp.src('src/**/*.twig')
        .pipe(twing(env))
        .pipe(rename(function(path) {
            if (path.basename.indexOf('.') > -1) {
                path.extname = '';
            }
        }))
        .pipe(gulp.dest('dest'))
}

// dest will contain foo.html, index.css and index.html

By combining gulp-rename and outputExt, you can compile index.css.twig, index.html.twig, foo.twig saving the output to index.css, index.html, and foo.ext:

// in gulpfile.js
var rename = require('gulp-rename');

// src contains foo.twig, index.css.twig and index.html.twig

function twig() {
    gulp.src('src/**/*.twig')
        .pipe(twing(env, {}, {outputExt: '.ext'}))
        .pipe(rename(function(path) {
            if (path.basename.indexOf('.') > -1) {
                path.extname = '';
            }
        }))
        .pipe(gulp.dest('dest'))
}

// dest will contain foo.ext, index.css and index.html

Contributing

  • Fork the main repository
  • Code
  • Implement tests using node-tap
  • Issue a pull request keeping in mind that all pull requests must reference an issue in the issue queue

License

Apache-2.0 © Eric MORAND