Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added 'locals' option, cleaned up the 'compile' helper a bit. #9

Merged
merged 2 commits into from

2 participants

@surjikal

Before, the options object was passed as the locals. I've added an explicit locals option, which can be either a function or an object.

One thing to note: If someone was relying on the locals being the options object, he will have to update his config accordingly.

I'm not sure how to add an example for this option, as it would require its own jade file.

@surjikal surjikal Added 'locals' option, cleaned up the 'compile' helper a bit.
Before, the 'options' object was passed as the locals. Now, there's
a dedicated 'locals' object. This change breaks backwards compatibility.

The 'locals' option can either be a function or an object.
797753f
@phated
Owner

I like this commit, but I don't plan to break the APIs until the 0.4 release. I plan to make 0.4 work with grunt 0.3.x and bring in this feature this weekend.

@surjikal

Sounds good!

@phated
Owner

For 0.4, I am going to pass the options object to the template if locals doesn't exist.

@surjikal

Oh good idea! In that case, you could even include it on master because it won't break the API.

@surjikal surjikal Merge branch 'master' into locals
Conflicts:
	README.md
	tasks/jade.js
2dcab51
@phated
Owner

It still needs testing and I want to give it another once-over, but I think I will be able to release a 0.3.9 that keeps the old API and allows for the new API and these features.

If you get a chance, take a look at 0.3.9 - https://github.com/phated/grunt-jade/commits/0.3.9

@phated phated merged commit 2dcab51 into phated:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 24, 2012
  1. @surjikal

    Added 'locals' option, cleaned up the 'compile' helper a bit.

    surjikal authored
    Before, the 'options' object was passed as the locals. Now, there's
    a dedicated 'locals' object. This change breaks backwards compatibility.
    
    The 'locals' option can either be a function or an object.
Commits on Nov 26, 2012
  1. @surjikal

    Merge branch 'master' into locals

    surjikal authored
    Conflicts:
    	README.md
    	tasks/jade.js
This page is out of date. Refresh to see the latest.
Showing with 55 additions and 15 deletions.
  1. +37 −0 README.md
  2. +18 −15 tasks/jade.js
View
37 README.md
@@ -114,7 +114,24 @@ jade: {
}
```
+For locals, add:
+
+```javascript
+jade: {
+ locals: {
+ src: ['path/to/src/*.jade'],
+ dest: 'dest/path/',
+ options: {
+ locals: {
+ title: 'Welcome to my website!'
+ }
+ }
+ }
+}
+```
+
For custom extension, add:
+
```javascript
jade: {
custom_extension: {
@@ -127,6 +144,25 @@ jade: {
}
```
+Or alternatively, use a function:
+
+```javascript
+jade: {
+ locals: {
+ src: ['path/to/src/*.jade'],
+ dest: 'dest/path/',
+ options: {
+ locals: function() {
+ return {compiledAt: new Date()};
+ }
+ }
+ }
+}
+```
+This is useful when you are also using the watch task, since the function will
+be called on each reload.
+
+
## Defaults
```javascript
@@ -134,6 +170,7 @@ options: {
client: true,
runtime: true,
compileDebug: false,
+ locals: {},
extension: null
}
View
33 tasks/jade.js
@@ -23,6 +23,7 @@ module.exports = function(grunt) {
client: true,
runtime: true,
compileDebug: false,
+ locals: {},
extension: null
}, this.data.options);
@@ -63,29 +64,31 @@ module.exports = function(grunt) {
// ==========================================================================
grunt.registerHelper('compile', function(src, options, wrapper, filename, filepath) {
+
var msg = 'Compiling' + (filepath ? ' ' + filepath : '') + '...';
grunt.verbose.write(msg);
+
var compiled = jade.compile(src, grunt.utils._.extend({
filename: filepath // required to use includes
}, options));
+
grunt.verbose.ok();
- var output;
+
// Was compilation successful?
- if(compiled){
- // Are we writing JS?
- if(options.client){
- compiled = String(compiled);
- // Are we wrapping it?
- if(wrapper.wrap){
- output = grunt.helper('wrap', compiled, wrapper, filename);
- } else {
- output = compiled;
- }
- } else {
- // Spit out
- output = compiled(options);
- }
+ if (!compiled) {
+ return;
}
+
+ var output;
+
+ if (options.client){
+ compiled = String(compiled);
+ output = wrapper.wrap? grunt.helper('wrap', compiled, wrapper, filename) : compiled;
+ } else {
+ var locals = grunt.utils._.isFunction(options.locals)? options.locals() : options.locals;
+ output = compiled(locals);
+ }
+
return output;
});
Something went wrong with that request. Please try again.