/node_modules/grunt/docs/api_template.md

https://bitbucket.org/jollyscience/grunt-init-templates · Markdown · 72 lines · 48 code · 24 blank · 0 comment · 0 complexity · 7be1d4d5ccc6fdcb505086c038fdc3cb MD5 · raw file

  1. [Grunt homepage](https://github.com/cowboy/grunt) | [Documentation table of contents](toc.md)
  2. # [The grunt API](api.md) / grunt.template
  3. Underscore.js template processing and other template-related methods.
  4. 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.
  5. See the [template lib source](../lib/grunt/template.js) for more information.
  6. ## The template API <a name="the-template-api" href="#the-template-api" title="Link to this section"></a>
  7. ### grunt.template.process <a name="grunt-template-process" href="#grunt-template-process" title="Link to this section"></a>
  8. 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.
  9. 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._
  10. 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)).
  11. ```javascript
  12. grunt.template.process(template, data, mode)
  13. ```
  14. In this example, the `baz` property is processed recursively until there are no more `<% %>` templates to process.
  15. ```javascript
  16. var obj = {
  17. foo: 'c',
  18. bar: 'b<%= foo %>d',
  19. baz: 'a<%= bar %>e'
  20. };
  21. grunt.template.process('<%= baz %>', obj) // 'abcde'
  22. ```
  23. ### grunt.template.delimiters <a name="grunt-template-delimiters" href="#grunt-template-delimiters" title="Link to this section"></a>
  24. 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.
  25. 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)).
  26. ```javascript
  27. grunt.template.delimiters(mode)
  28. ```
  29. ## Template Helpers <a name="template-helpers" href="#template-helpers" title="Link to this section"></a>
  30. ### grunt.template.date <a name="grunt-template-date" href="#grunt-template-date" title="Link to this section"></a>
  31. Format a date using the [dateformat](https://github.com/felixge/node-dateformat) library.
  32. ```javascript
  33. grunt.template.date(date, format)
  34. ```
  35. In this example, a specific date is formatted as month/day/year.
  36. ```javascript
  37. grunt.template.date(847602000000, 'yyyy-mm-dd') // '1996-11-10'
  38. ```
  39. ### grunt.template.today <a name="grunt-template-today" href="#grunt-template-today" title="Link to this section"></a>
  40. Format today's date using the [dateformat](https://github.com/felixge/node-dateformat) library.
  41. ```javascript
  42. grunt.template.today(format)
  43. ```
  44. In this example, today's date is formatted as a 4-digit year.
  45. ```javascript
  46. grunt.template.today('yyyy') // '2012'
  47. ```
  48. _(somebody remind me to update this date every year so the docs appear current)_