Closed buschtoens closed 4 years ago
I am working on a project at the moment where this feature would be really useful.
If I was able to find the mixin definition in the source I'd do a pull request. I'll search for it in a few hours, when I'm done doing some client-work.
Actually you can do the following:
mixin say(something)
- something = something || 'hello'
p #{something}
Cool! But the default value would be some extra syntactic sugar that's worth implememnting. :D
I'm inclined to say we should deffer this until ES6 has been finalized, and then make sure that in ES6 environments, we support ES6 syntax.
Oh, didn't know, that ES6 will support parameter default values. Good to know. :) But is this really linked to the ES6 syntax?
Searching for the code...
Ah, finally found the corresponding code. Haha. Yeah, currently we'd have to wait for ES6. But I really feel like this should be added to the jade syntax itself and should not rely on ES6. For instance, this will cause problems, when people start exchanging ES6 jade code and try to run it in ES5 enviroments.
We could consider using an ES6 transpiler on the compiled function (like http://code.google.com/p/traceur-compiler/) but I still don't think we should do so until ES6 is fairly commonplace.
Incidentally, it's worth looking at http://es6isnigh.com/assets/presentation-full.pdf for @domenic's summary of ES6 features. A lot of the syntax will change, but the broad feature set is pretty much set now.
@revington, is this
mixin say(something)
- something = something || 'hello'
p #{something}
correct, if a value could be false
or ""
?
Maybe better:
mixin say(something)
- something = typeof something != "undefined" ? something : 'hello'
p #{something}
?
will this feature be implemented? ES6 is a standard now.
@Grawl this feature already works in a ES2015-supported runtime. Unfortunately, io.js/V8 doesn't support ES2015 default parameters yet, so you can't blame us.
For example (with current jade and jade-cli master):
//- without-default.jade
mixin a(b)
p= b
+a()
//- with-default.jade
mixin a(b = 10)
p= b
+a()
$ jade --client --no-debug -O '{"externalRuntime":true}' without-default.jade
$ jade --client --no-debug -O '{"externalRuntime":true}' with-default.jade
$ js-beautify without-default.js | sponge without-default.js
$ js-beautify with-default.js | sponge with-default.js
$ diff -uw without-default.js with-default.js >out.diff
(some noise removed)
--- without-default.js 2015-08-30 20:38:33.664840871 -0700
+++ with-default.js 2015-08-30 20:38:23.808840871 -0700
@@ -4,18 +4,21 @@
jade_interp;
var jade_debug_filename, jade_debug_line;
try {;
jade_debug_line = 2;
- jade_debug_filename = "without-default.jade";
- jade_mixins["a"] = jade_interp = function(b) {
+ jade_debug_filename = "with-default.jade";
+ jade_mixins["a"] = jade_interp = function(b = 10) {
var block = (this && this.block),
attributes = (this && this.attributes) || {};;
jade_debug_line = 3;
- jade_debug_filename = "without-default.jade";
+ jade_debug_filename = "with-default.jade";
jade_html = jade_html + "<p>" + (jade.escape(null == (jade_interp = b) ? "" : jade_interp)) + "</p>";
};;
jade_debug_line = 4;
- jade_debug_filename = "without-default.jade";
+ jade_debug_filename = "with-default.jade";
jade_mixins["a"]();
} catch (err) {
jade.rethrow(err, jade_debug_filename, jade_debug_line);
};
notice the added function(b = 10)
– a sign that Jade passes default parameters through
Current browsers support 98% of ES6 now. Is this feature on the todo list?
As @TimothyGu says, this already "Just Works" but needs documenting in https://github.com/pugjs/pug-en/blob/master/src/language/mixins.md
Super useful feature.
Why is this still open?
https://github.com/pugjs/pug-en/blob/master/src/language/mixins.md#default-arguments-values
It would be nice to set default values for arguments in mixins.