Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including grunt-twig2html with all npm packages installed. Try it out:

require("grunt-cli/package.json"); // grunt-cli is a peer dependency. var gruntTwig2html = require("grunt-twig2html")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

grunt-twig2html v1.3.2

Grunt plugin that compile twig templates to html pages

Build Status

grunt-twig2html

Grunt plugin that compile twig templates to html pages.

Build upon Twig.js, the JS port of the Twig templating language by John Roepke.

Getting Started

This plugin requires Grunt ^1.0.1

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-twig2html --save-dev

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

grunt.loadNpmTasks('grunt-twig2html');

The "twig2html" task

Overview

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

grunt.initConfig({
  twig2html: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    }
  }
});

Options

options.globals

Type: String Default value: ''

Path to JSON file with global context variables.

options.extensions

Type: Array Default value: []

Can be an array of functions that extend TwigJS with custom tags.

options.functions

Type: Object Default value: {}

Object hash defining functions in TwigJS.

options.filters

Type: Object Default value: {}

Object hash defining filters in TwigJS.

options.context

Type: Object Default value: {}

Object hash defining templates context variables.

options.separator

Type: String Default value: '\n'

A string that is inserted between each compiled template when concatenating templates.

Usage Examples

grunt.initConfig({
    twig2html: {
        options: {
            context: {}, // task specific context object hash
            globals: 'path/to/globals.json'
        },
        target: {
            options: {
                context: {} // target specific context object hash
            },
            files: [
                {
                    expand: true,
                    cwd: 'path/to/pages',
                    src: ['**/*.twig'],
                    dest: 'path/to/dest/',
                    ext: '.html'
                }
            ]
        }
    }
});

Context hierarchy

Template context extends in this order:

  • options.globals or target.options.globals if provided
  • options.context or target.options.context if provided
  • template JSON context files (stored in template path, with same name, example: /templates/index.json for /templates/index.twig) if provided

Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Sponsored by

DesignDepot

Release History

See the CHANGELOG.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free