• Stars
    star
    329
  • Rank 123,758 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compile Pug templates.

grunt-contrib-pug v3.0.0 Build Status

Compile Pug templates

Getting Started

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-contrib-pug --save-dev

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

grunt.loadNpmTasks('grunt-contrib-pug');

Pug task

Run this task with the grunt pug command.

Task targets, files and options may be specified according to the grunt Configuring tasks guide.

Options

pretty

Type: Boolean
Default: false

Output indented HTML.

data

Type: Object

Sets the data passed to Pug during template compilation. Any data can be passed to the template (including grunt templates).

This value also might be a function taking source and destination path as arguments and returning a data object. Within the function, this is bound to the file configuration object.

options: {
  data: function(dest, src) {
    return {
      from: src,
      to: dest
    };
  }
}

or you can have options from a required JSON file:

options: {
  data: function(dest, src) {
    // Return an object of data to pass to templates
    return require('./locals.json');
  }
}

filters

Type: Object

If you want to use filters you have two ways to do it. First you can write your filters inline within your Gruntfile.js or define filters in separate file and export it.

Filters are given a context with the pug instance and local variables: {pug: pug, data: data}, where pug is global pug instance and data is options passed to options.data. You can use this.pug.render() inside your filters to render the content of a block and locals as #{variable} from your data.

Inline filters

Gruntfile.js:

options: {
  filters: {
    some: function(block) {},
    another: function(block) {}
  }
}
Exported filters

Gruntfile.js:

options: {
  filters: require('./filters.js')
}

filters.js:

var pugfilters = module.exports = {};
pugfilters.some = function(block) {};
pugfilters.another = function(block) {};

compileDebug

Type: Boolean
Default: true

Add Pug debug instructions to generated JS templates.

client

Type: Boolean
Default: false

Compile to JS template functions for client-side use rather than directly to HTML.

Make sure to also include the Pug runtime (only runtime.js) as described in the Pug documentation.

namespace

Type: String, Boolean
Default: 'JST'

The namespace in which the precompiled templates will be assigned. Use dot notation (e.g. App.Templates) for nested namespaces or false for no namespace wrapping.

When set to false with amd option set to true, the templates will be returned directly from the AMD wrapper.

amd

Type: Boolean
Default: false

Wraps the output file with an AMD define function and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly.

define(function() {
    //...//
    returns this['[template namespace]'];
});

processName

Type: Function

This option accepts a function which takes one argument (the template filepath) and returns a string which will be used as the key for the precompiled template object.

Example Store all template on the default JST namespace in capital letters.

options: {
  processName: function(filename) {
    return filename.toUpperCase();
  }
}

processContent

Type: Function Default: function(content, filename) { return content; };

This option accepts a function that lets you perform additional content processing.

Usage Examples

pug: {
  compile: {
    options: {
      data: {
        debug: false
      }
    },
    files: {
      'path/to/dest.html': ['path/to/templates/*.pug', 'another/path/tmpl.pug']
    }
  }
}

If you want to generate a debug file and a release file from the same template:

pug: {
  debug: {
    options: {
      data: {
        debug: true
      }
    },
    files: {
      'debug.html': 'test.pug'
    }
  },
  release: {
    options: {
      data: {
        debug: false
      }
    },
    files: {
      'release.html': 'test.pug'
    }
  }
}

If you want to use grunt template in options.data:

pug: {
  debug: {
    options: {
      data: {
        debug: true,
        timestamp: '<%= new Date().getTime() %>'
      }
    },
    files: {
      'debug.html': 'test.pug'
    }
  }
}

or you can use grunt helpers (grunt object was exposed at template context):

pug: {
  debug: {
    options: {
      data: {
        debug: true,
        timestamp: '<%= grunt.template.today() %>'
      }
    },
    files: {
      'debug.html': 'test.pug'
    }
  }
}

Release History

  • 2022-04-12   v3.0.1   Update dependencies.
  • 2021-02-23   v3.0.0   Update to pug 3.0.0 Drop Node.js < 10 support.
  • 2018-09-07   v2.0.0   Drop Node.js < 6 support. Update dependencies.
  • 2016-03-04   v1.0.0   Point main to task and remove peerDeps. Update docs and tests.
  • 2015-07-08   v0.15.0   Update to jade 1.11.0. Add test for Codeblocks
  • 2014-02-02   v0.14.1   Add filename to processContent arguments.
  • 2014-12-23   v0.14.0   Update to jade 1.8.2.
  • 2014-09-30   v0.13.0   Update to jade 1.7.0.
  • 2014-05-29   v0.12.0   Update to jade 1.3. Make jade task fail on an error.
  • 2014-03-02   v0.11.0   Document processContent. Bump to jade 1.2. Update copyright to 2014. Remove lodash-node module.
  • 2014-01-20   v0.10.0   Bump jade version to ~1.1.5. Fix AUTHORS.
  • 2014-01-04   v0.9.1   Bump jade version to 1.0.2. Use node-lodash instead of grunt.util._.
  • 2013-12-24   v0.9.0   Bump jade version to 1.0.0.
  • 2013-07-29   v0.8.0   Bump jade version to 0.34.1.
  • 2013-06-06   v0.7.0   Bump jade version / fix tests.
  • 2013-05-15   v0.6.0   Bump jade version / fix tests.
  • 2013-05-06   v0.5.1   Allow options.data to be a function.
  • 2013-03-07   v0.5.0   Allow compilation to JS functions. Support JST and AMD formats.
  • 2013-02-15   v0.4.0   First official release for Grunt 0.4.0.
  • 2013-01-24   v0.4.0rc7   Updating grunt/gruntplugin dependencies to rc7. Changing in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions.
  • 2013-01-09   v0.4.0rc5   Updating to work with grunt v0.4.0rc5. Switching to this.files API.
  • 2012-10-12   v0.3.1   Rename grunt-contrib-lib dep to grunt-lib-contrib.
  • 2012-09-24   v0.3.0   Options no longer accepted from global config key.
  • 2012-09-10   v0.2.0   Refactored from grunt-contrib into individual repo.

Task submitted by Eric Woroshow

This file was generated on Tue Apr 12 2022 08:42:31.

More Repositories

1

grunt

Grunt: The JavaScript Task Runner
JavaScript
12,255
star
2

grunt-contrib-watch

Run tasks whenever watched files change.
JavaScript
1,981
star
3

grunt-contrib-uglify

Minify files with UglifyJS.
JavaScript
1,481
star
4

grunt-contrib-imagemin

Minify PNG, JPG, GIF and SVG images.
JavaScript
1,214
star
5

grunt-contrib-sass

Compile Sass to CSS.
JavaScript
848
star
6

grunt-contrib-concat

Concatenate files.
JavaScript
809
star
7

grunt-contrib-cssmin

Compress CSS files.
JavaScript
795
star
8

grunt-contrib-copy

Copy files and folders.
JavaScript
729
star
9

grunt-contrib-connect

Start a static web server.
JavaScript
717
star
10

grunt-contrib-jshint

Validate files with JSHint.
JavaScript
710
star
11

grunt-cli

Grunt's command line interface.
JavaScript
708
star
12

grunt-contrib-less

Compile LESS files to CSS.
JavaScript
671
star
13

grunt-contrib-compass

Compile Compass to CSS.
JavaScript
626
star
14

grunt-contrib-clean

Clear files and folders.
JavaScript
516
star
15

grunt-contrib-requirejs

Optimize RequireJS projects using r.js.
JavaScript
504
star
16

grunt-contrib

[DEPRECATED] A collection of common grunt tasks.
JavaScript
476
star
17

grunt-contrib-htmlmin

Minify HTML.
JavaScript
427
star
18

grunt-contrib-jasmine

Run jasmine specs headlessly through Headless Chrome
JavaScript
356
star
19

grunt-contrib-compress

Compress files and folders.
JavaScript
346
star
20

grunt-contrib-coffee

Compile CoffeeScript files to JavaScript.
JavaScript
330
star
21

grunt-contrib-handlebars

Precompile Handlebars templates to JST file.
JavaScript
282
star
22

grunt-contrib-csslint

Lint CSS files.
JavaScript
242
star
23

grunt-contrib-qunit

Run QUnit tests in Headless Chrome.
JavaScript
215
star
24

grunt-contrib-livereload

Reload assets live in the browser.
JavaScript
203
star
25

grunt-init

Generate project scaffolding from a template.
JavaScript
191
star
26

grunt-contrib-stylus

Compile Stylus files to CSS.
JavaScript
175
star
27

grunt-init-gruntfile

Create a basic Gruntfile with grunt-init.
JavaScript
156
star
28

gruntjs.com

Grunt's Website
Less
156
star
29

grunt-contrib-jst

Compile underscore templates to JST file.
JavaScript
113
star
30

grunt-lib-phantomjs

Grunt and PhantomJS, sitting in a tree.
JavaScript
94
star
31

grunt-next

Grunt v1.0 alpha
JavaScript
84
star
32

grunt-docs

Grunt documentation.
80
star
33

grunt-init-gruntplugin

Create a gruntplugin module with grunt-init, including Nodeunit unit tests.
JavaScript
79
star
34

grunt-contrib-yuidoc

Compile YUIDoc Documentation.
JavaScript
76
star
35

grunt-contrib-nodeunit

Run Nodeunit unit tests.
JavaScript
69
star
36

grunt-contrib-symlink

Create symbolic links.
JavaScript
58
star
37

grunt-init-jquery

Create a jQuery plugin with grunt-init, including QUnit unit tests.
JavaScript
58
star
38

grunt-init-node

Create a Node.js module with grunt-init, including Nodeunit unit tests.
JavaScript
54
star
39

grunt-contrib-bump

A work-in-progress Grunt plugin for bumping a version number in JSON files.
JavaScript
52
star
40

grunt-init-commonjs

Create a commonjs module with grunt-init, including Nodeunit unit tests.
JavaScript
17
star
41

grunt-contrib-internal

Internal tasks for managing the grunt-contrib project.
JavaScript
16
star
42

grunt-init-gruntfile-sample

This is sample output generated by the grunt-init "gruntfile" template.
JavaScript
14
star
43

grunt-init-jquery-sample

This is sample output generated by the grunt-init "jquery" template.
JavaScript
12
star
44

grunt-lib-contrib

Common functionality shared across grunt-contrib tasks.
JavaScript
12
star
45

example-subgrunt

Run a Gruntfile in multiple subdirectories.
JavaScript
11
star
46

grunt-contrib-mincss

Renamed to grunt-contrib-cssmin.
9
star
47

grunt-init-gruntplugin-sample

This is sample output generated by the grunt-init "gruntplugin" template.
JavaScript
9
star
48

grunt-init-node-sample

This is sample output generated by the grunt-init "node" template.
JavaScript
8
star
49

grunt-known-options

The known options used in Grunt
JavaScript
7
star
50

grunt-init-commonjs-sample

This is sample output generated by the grunt-init "commonjs" template.
JavaScript
7
star
51

clone-repos

Quickly clone all gruntjs repos (for grunt development)
Ruby
6
star
52

grunt-lib-legacyhelpers

Some old grunt helpers provided for backwards compatability.
JavaScript
6
star
53

grunt-legacy-util

deprecated utility methods
JavaScript
5
star
54

grunt-legacy-log

The Grunt logger.
JavaScript
5
star
55

rfcs

RFCs for changes to Grunt
4
star
56

grunt-legacy-log-utils

Static methods for the Grunt 0.4.x logger.
JavaScript
4
star
57

grunt-plugin-list

[Deprecated] Generates a list of all grunt plugins as json
JavaScript
4
star
58

grunt-legacy-event-logger

Event logger for Grunt legacy libs.
JavaScript
3
star
59

grunt-legacy-config

Grunt's config methods, as a standalone library.
JavaScript
2
star
60

grunt-legacy-task

Grunt's task methods, as a standalone library.
JavaScript
1
star
61

grunt-legacy-option

Grunt's option methods, as a standalone library.
JavaScript
1
star
62

grunt-legacy-cli

Grunt's CLI methods, as a standalone library.
JavaScript
1
star