Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 40 lines (25 sloc) 2.454 kB
b38875e @cowboy Updating docs.
authored
1 [Grunt homepage](https://github.com/cowboy/grunt) | [Documentation table of contents](toc.md)
cf8e19f @cowboy More documentation.
authored
2
3 # Helpers and Directives
4
6dcfaca @cowboy Adding helpers and directives documentation.
authored
5 ## Helpers
6 Helpers are just utility functions, exposed through the `task` global variable, so that they can be used by tasks in other files.
7
8 It's not much more complex than this:
9
10 ```javascript
11 task.registerHelper('foo', function(a, b) {
12 return a + b;
13 });
14
15 task.helper('foo', 2, 3) // 5
16 ```
17
24f2be1 @cowboy More docs.
authored
18 For example, in the [min task](../tasks/min.js), the majority of the actual minification work is done in an `uglify` helper, so that other tasks can utilize that code if they need to.
6dcfaca @cowboy Adding helpers and directives documentation.
authored
19
1101421 @cowboy More docs.
authored
20 Take a look at the [built-in tasks source code](../tasks) for more examples.
e510686 @cowboy Moving example gruntfiles to their own docs file.
authored
21
6dcfaca @cowboy Adding helpers and directives documentation.
authored
22 ## Directives
7943e19 @cowboy More 0.3.0 docs updates.
authored
23 Directives are essentially string placeholders for helper functions, specified as values in the [configuration object](configuring.md). It sounds crazy, but it's not as crazy as it sounds.
6dcfaca @cowboy Adding helpers and directives documentation.
authored
24
24f2be1 @cowboy More docs.
authored
25 A good example of directives would be the `<json:package.json>` and `<config:lint.all>` directives in grunt's own [grunt.js gruntfile](../grunt.js). Or the `<banner>` and `<file_strip_banner:src/grunt-jquery-example.js>` directives in the [sample jQuery plugin gruntfile](https://github.com/cowboy/grunt-jquery-example/blob/master/grunt.js).
6dcfaca @cowboy Adding helpers and directives documentation.
authored
26
2ada036 @cowboy Adding some edited docs. Note that many changes in these disc apply t…
authored
27 In brief, when a directive like `<foo>` is encountered, the `foo` helper is executed, and its return value is substituted in place of the directive string. If `<foo:bar:baz>` is encountered, the `foo` helper is executed, with arguments `"bar"` and `"baz"` passed in.
6dcfaca @cowboy Adding helpers and directives documentation.
authored
28
2ada036 @cowboy Adding some edited docs. Note that many changes in these disc apply t…
authored
29 A few of the built-in directives:
6dcfaca @cowboy Adding helpers and directives documentation.
authored
30
7943e19 @cowboy More 0.3.0 docs updates.
authored
31 * `<config:prop.subprop>` - expand to the `prop.subprop` config property. This can be any number of objects deep, `prop.subprop.otherprop.whatever` is totally valid. Great for DRYing up file lists.
6dcfaca @cowboy Adding helpers and directives documentation.
authored
32 * `<json:file.json>` - expand to the object parsed from file.json (a valid JSON file).
1e0ac5a @cowboy More directives docs tweaks.
authored
33 * `<banner>` - the string in config property `meta.banner`, parsed via [Underscore.JS template](http://underscorejs.org/#template), using `<% %>` delimiters.
6dcfaca @cowboy Adding helpers and directives documentation.
authored
34 * `<banner:prop.subprop>` - same as above, but using a custom config property.
7943e19 @cowboy More 0.3.0 docs updates.
authored
35 * `<file_strip_banner:file.js>` - expand to the given file, with any leading `/*...*/` banner stripped (note that this directive will not strip leading `/*!...*/` banners).
36
37 _Note: for simplicity's sake, directives may only use synchronous helpers. If an asynchronous helper is referenced, it will not work!_
e510686 @cowboy Moving example gruntfiles to their own docs file.
authored
38
2ada036 @cowboy Adding some edited docs. Note that many changes in these disc apply t…
authored
39 Take a look at the [example gruntfiles](example_gruntfiles.md) for directive usage examples.
Something went wrong with that request. Please try again.