简体   繁体   English

哈巴狗/玉按名称指代财产

[英]pug/jade refer to property by name

I want to check if some property names are passed to the template based on a preset array: 我想检查是否根据预设数组将某些属性名称传递给模板:

- socialNames = ['facebook', 'twitter', 'instagram']
each socialName in socialNames
    - social = this[socialName]
    if social 
        a(href=social.url)
            div
                img.ug-profile-socials-
icon(src=chrome.extension.getURL('public/socials/'+ social + '.png'))
            div
                span.ug-profile-socials-followers !{social.followers}

The social = this[socialName] part is just the way I was expecting it to work, but it obviously does not. social = this [socialName]部分只是我期望它工作的方式,但显然没有。

You need not use this to refer to individual items in the array. 您无需使用this来引用数组中的单个项目。 You can directly reference the array items using the variable you mentioned in your each statement ( socialName in your case ): 您可以使用each statement提到的变量(在本例中为socialName )直接引用数组项:

- socialNames = ['facebook', 'twitter', 'instagram']
each socialName in socialNames
    // - social = this[socialName]
    social = socialName
    a(href=social.url)
        div
            img.ug-profile-socials--icon(src=chrome.extension.getURL('public/socials/'+ social + '.png'))
        div
            span.ug-profile-socials-followers !{social.followers}

Seems that pug uses locals to refer to the passed arguments: 似乎pug使用locals来引用传递的参数:

- var social = locals[socialName]
if social
    a(href=social.url)
        div.ug-profile-socials-icon
            img(src=chrome.extension.getURL('public/socials/'+ socialName + '.png'))
        div.ug-profile-socials-followers
            span !{social.followers}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM