问题
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" />
回答1:
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"/>
回答2:
You need to pass a object name or use the keyword 'global' like this:
partial('myview', { {
name:'username',
value:'bob',
attributes: {
maxlength: 16
}
}, as: global });
otherwise you need to give your object a name and access them through that scope.
Check out the docs
来源:https://stackoverflow.com/questions/10864196/expanding-an-object-to-set-attributes-in-jade