Extending an object to set attributes in Jade

I would like to be able to pass an object with key / value pairs that represent the attributes for the element. Is this possible with Jade?

Any solution that allows me to pass a collection of attributes into my template would be sufficient, but it would be ideal to combine explicitly declared attributes with attributes retrieved from the object (as shown below).

The following syntax does not work, this is just an example of what I would like to do.

For example, if I passed this:

{ name:'username', value:'bob', attributes: { maxlength: 16 } } 

To this template:

 input(name=name, value=value, attributes) 

Desired Result:

 <input name="username" value="bob" maxlength="16" /> 
+7
source share
2 answers

Since you are part of this issue on GitHub, you probably already know the answer. But, for someone else, here is the answer:

Jade:

 input.foo(name=name value=value)&attributes(attrs) 

Pass this data into your render function:

 { name: 'username', value: 'bob', attrs: { maxlength: 16 } } 

Output:

 <input name="username" value="bob" class="foo" maxlength="16"/> 
+5
source

You need to pass the name of the object or use the 'global' keyword as follows:

 partial('myview', { { name:'username', value:'bob', attributes: { maxlength: 16 } }, as: global }); 

otherwise, you need to give your object a name and access them through this area.

Check documents

0
source

All Articles