Expanding an object to set attributes in Jade

帅比萌擦擦* 提交于 2019-12-06 23:37:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!