Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jade mixin default parameters

Tags:

pug

I have a following mixin in Jade:

mixin indicator(slide_to, active)
  - active = active || '' // this is an ugly method to create a default value
  li(class=active, data-target='#' + CAROUSEL_ID, data-slide-to=slide_to)

and I call it like this:

+indicator(1) 
+indicator(2, 'active')

I want the parameter active to have a default value of ''. I have found the ugly workaround as is shown in my codesample. Is there a better way to do this in Jade?

like image 771
netimen Avatar asked Feb 14 '23 14:02

netimen


1 Answers

This example:

mixin test(slide_to, active)
    li(class=active, data-slide-to=slide_to)

+test('a')
+test('a', 'b')

Compiles into this HTML for me:

<li data-slide-to="a"></li>
<li data-slide-to="a" class="b"></li>

I'm using Jade 0.35.0.

Jade doesn't add falsy values. You can read this in the Jade reference in the subchapter Boolean Attributes.

like image 137
Sascha Wolf Avatar answered Apr 06 '23 10:04

Sascha Wolf