Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding an object to set attributes in Jade

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

Any solution that allows me to pass an attributes collection into my template would be sufficient, but the ability to mix explicitly declared attributes with attributes extracted from an object (as below) would be ideal.

The following syntax does not work, it is just an example of what I'd 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)

The desired output would be:

<input name="username" value="bob" maxlength="16" />
like image 879
Prestaul Avatar asked Jun 02 '12 16:06

Prestaul


1 Answers

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

Jade:

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

Pass this data to your render function:

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

Output:

<input name="username" value="bob" class="foo" maxlength="16"/>
like image 132
Jonathan Avatar answered Oct 13 '22 00:10

Jonathan