8fdf08ab772256351830cac6542d40d53a030e58.svn-base
2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
[Grunt homepage](https://github.com/gruntjs/grunt) | [Documentation table of contents](toc.md)
# [The grunt API](api.md) / grunt.template
Underscore.js template processing and other template-related methods.
Template strings can be processed manually using the provided template functions. In addition, many tasks and helpers automatically expand `<% %>` style template strings specified inside the [grunt.js gruntfile](getting_started.md) when used as file paths and banners.
See the [template lib source](../lib/grunt/template.js) for more information.
## The template API
### grunt.template.process
Process an [Underscore.js template](http://underscorejs.org/#template) string. If `data` is omitted, the entire [config object](api_config.md) is used. Templates are processed recursively until there are no more templates to process.
Inside templates, the `grunt` object is exposed as `grunt` so that you can do things like `<%= grunt.template.today('yyyy') %>`. _Note that if the `data` object has a `grunt` property, it will prevent this from working._
If `mode` is omitted, `<% %>` style template delimiters will be used. If `mode` is "init", `{% %}` style template delimiters will be used (this is specifically used by the [init task](task_init.md)).
```javascript
grunt.template.process(template, data, mode)
```
In this example, the `baz` property is processed recursively until there are no more `<% %>` templates to process.
```javascript
var obj = {
foo: 'c',
bar: 'b<%= foo %>d',
baz: 'a<%= bar %>e'
};
grunt.template.process('<%= baz %>', obj) // 'abcde'
```
### grunt.template.delimiters
Set [Underscore.js template](http://underscorejs.org/#template) delimiters manually, in case you need to use `grunt.utils._.template` manually. You probably won't need to call this, because you'll be using `grunt.template.process` which calls this internally.
If `mode` is omitted, `<% %>` style template delimiters will be used. If `mode` is "init", `{% %}` style template delimiters will be used (this is specifically used by the [init task](task_init.md)).
```javascript
grunt.template.delimiters(mode)
```
## Template Helpers
### grunt.template.date
Format a date using the [dateformat](https://github.com/felixge/node-dateformat) library.
```javascript
grunt.template.date(date, format)
```
In this example, a specific date is formatted as month/day/year.
```javascript
grunt.template.date(847602000000, 'yyyy-mm-dd') // '1996-11-10'
```
### grunt.template.today
Format today's date using the [dateformat](https://github.com/felixge/node-dateformat) library.
```javascript
grunt.template.today(format)
```
In this example, today's date is formatted as a 4-digit year.
```javascript
grunt.template.today('yyyy') // '2012'
```
_(somebody remind me to update this date every year so the docs appear current)_