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

grunt-twigger

v1.0.1

Published

Easy way to turn data and/or twig templates into HTML.

Downloads

5

Readme

grunt-twigger

Easy way to turn data and/or Twig templates into HTML.

Getting Started

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-twigger --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-twigger');

The "twigger" task

Overview

In your project's Gruntfile, add a section named twigger to the data object passed into grunt.initConfig().

grunt.initConfig({
  twigger: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      options: {
        // Target-specific options go here.
      },
      // Paramaters go here.
    },
  },
});

Usage Examples

Turn a Twig template into HTML

This renders a single Twig template file into a single HTML file, with some data:

grunt.initConfig({
  twigger: {
    your_target: {
      src: 'path/to/template.twig',
      data: {
        foo: 2,
        bar: 'Variables available in the template!'
      },
      dest: 'path/to/output.html'
    }
  },
});

Data can also be provided as a JSON or YAML file:

grunt.initConfig({
  twigger: {
    your_target: {
      src: 'path/to/template.twig',
      data: 'path/to/data.json', // or data.yaml
      dest: 'path/to/output.html'
    }
  },
});

Multiple data sources can be merged together via an array. The last item in the array takes precedence, and will overwrite values preceding it.

grunt.initConfig({
  twigger: {
    your_target: {
      src: 'path/to/template.twig',
      data: [{ foo: 2 }, 'path/to/data1.json', 'path/to/data2.yaml'],
      dest: 'path/to/output.html'
    }
  },
});

Turn multiple Twig templates into HTML

You can render multiple Twig templates at once and place them into an output directory using regular grunt src expansion. Each Twig template will have access to the data passed in via the data parameter.

grunt.initConfig({
  twigger: {
    your_target: {
      src: 'path/to/templates/*.twig',
      data: 'path/to/data.json',
      dest: 'path/to/output/'
    }
  },
});

Turn multiple data files into HTML

By selecting multiple JSON or YAML data files as the src, and specifying a template parameter, twigger can output several HTML files based on the same template but with different data. In this way, twigger can be used as a simple static site generator.

grunt.initConfig({
  twigger: {
    your_target: {
      src: 'path/to/data/*.json',
      template: 'path/to/template.twig',
      dest: 'path/to/output/'
    }
  },
});

Options

options.twig

Type: Object Default value: { cache: false }

Params object passed into the twig.js twig function when rendering the template. Use this to adjust twig.js settings.

The path, async and rethrow params are not configurable.

An additional cache param is provided to enable/disable twig.js caching. Caching is off by default.

For example, to specify a base path for twig {{ include }}s:

grunt.initConfig({
  twigger: {
    options: {
      twig: {
        base: 'includes'
      }
    },
    // ...
  }
});

options.data

Type: Object Default value: {}

Additional data that available to all Twig templates rendered by this task.

options.preRender

Type: Function Default value: none

A function called before each Twig template is rendered. The function is passed a single params object with the keys:

  • data – the final data that will be passed into the Twig template. Can be modified.
  • src – the path to the Twig template or JSON/YAML file that that grunt is processing.
  • template – the path to the Twig template being rendered.

The preRender hook is useful for providing templates with dynamic variables. For example, to provide a Twig template with a variable called template containing its own template path, one could write:

grunt.initConfig({
  twigger: {
    options: {
      preRender: function(params) {
        params.data.template = params.template;
      }
    },
    // ...
  }
});

Parameters

src

Grunt path specifying the source Twig template files or JSON/YAML files to generate from. If JSON/YAML files are specified, a template parameter must also be specified.

dest

Path to a file or folder to put output into. If there is a single source file, an output file should be specified. If there are multiple source files, a directory should be specified.

data

Additional data given to the Twig template before rendering. Can be a JavaScript object literal, grunt path to a JSON/YAML file or an array of literals and paths.

Data sources at a higher index in the list take precedence over sources left of it. If data is specified in the src parameter, it takes precedence over all sources in the data parameter.Paths to JSON/YAML files are expanded using regular grunt globbing.

template

The path to the Twig template to use, if src points to a JSON/YAML data file. Only one template may be specified.

Contributing

Please take care to maintain the existing coding style. Add unit tests for any new functionality. Run tests using grunt test.

Release History

  • 1.0.0 – Initial release