pugjs / pug

Pug – robust, elegant, feature rich template engine for Node.js
https://pugjs.org
21.69k stars 1.95k forks source link

Default values for arguments in mixins #743

Closed buschtoens closed 4 years ago

buschtoens commented 12 years ago

It would be nice to set default values for arguments in mixins.

mixin lorem(count = 10)
  - for(var i = 0; i < count; i++) {
  p Lorem Ipsum dolor sit amet...
  - }

h1 Here is some text for you
mixin lorem() // count = 10

h1 And here's a little less
mixin lorem(3) // count = 3
William-Owen commented 12 years ago

I am working on a project at the moment where this feature would be really useful.

buschtoens commented 12 years ago

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.

revington commented 12 years ago

Actually you can do the following:

mixin say(something)
   - something = something || 'hello'
   p #{something}
buschtoens commented 12 years ago

Cool! But the default value would be some extra syntactic sugar that's worth implememnting. :D

ForbesLindesay commented 11 years ago

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.

buschtoens commented 11 years ago

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.

ForbesLindesay commented 11 years ago

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.

ForbesLindesay commented 11 years ago

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.

seleckis commented 9 years ago

@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}

?

Grawl commented 9 years ago

will this feature be implemented? ES6 is a standard now.

TimothyGu commented 9 years ago

@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

aleclarson commented 6 years ago

Current browsers support 98% of ES6 now. Is this feature on the todo list?

ForbesLindesay commented 6 years ago

As @TimothyGu says, this already "Just Works" but needs documenting in https://github.com/pugjs/pug-en/blob/master/src/language/mixins.md

admodev commented 4 years ago

Super useful feature.

trasherdk commented 4 years ago

Why is this still open?

https://github.com/pugjs/pug-en/blob/master/src/language/mixins.md#default-arguments-values