Can I pass a Jade mixin variable with multiple arguments?

I would like to create a variable containing all the mixin arguments, and then pass the variable to mixin. Is this possible, and if so, why doesn't it work?

- var foo = "'arg1','arg2','arg3'" mixin bar(arg1, arg2, arg3) div #{arg1}, #{arg2}, #{arg3} +bar(foo) 
+4
source share
2 answers

What you are trying does not work because you are using a string as the only argument and calling your mixin drum with one string. You do not share string arguments with the ',' character, so only your arg1 contains a value.

First let me fix your mixin bar

 mixin bar(args) div each arg in args | #{arg} 

The code uses only one argument named "args". Some type of enumerated type or array type is expected for this argument. See the jade documentation for more information on iterations . Code block | #{arg} | #{arg} writes the arg value as plain text.

How to call this mixin? Instead of providing an argument as a string, you need to provide an array of arguments:

 mixin bar(['arg1','arg2','arg3']) 

If you want to pass a string as an argument, you will have to rewrite the mixing panel:

 mixin barString(argString) - args = argString.split(','); div each arg in args | #{arg} 

Then you can call mixString barString:

 mixin barString("arg1,arg2,arg3") 

Note that I removed the single quotes for this call.

+6
source

Late, you can also do it like this

 mixin bar(...args) each arg in args | #{arg} +bar('arg1', 'arg2', 'arg3') 
0
source

All Articles