在pyjade中使用python代码 [英] Using python code in pyjade

查看:168
本文介绍了在pyjade中使用python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pyjade生成列表,如下所示:

I'm trying to generate a list using pyjade, like so:

ul
  - for i, (label, link) in enumerate(tabs)
    li(class="selected" if i == selected_index else "")
      a(href=link)= label

但是我看到这个错误:

UndefinedError:枚举"未定义

UndefinedError: 'enumerate' is undefined

我必须将python代码嵌入到Jade中是错误的.什么是正确的方法?

I must be embedding python code into Jade wrong. What's the right way to do this?

推荐答案

Jade使用我称为隐式枚举"的方法-它仅通过添加一个比存在值多的变量i来枚举列表中的值.打开包装:for item, i in list_like(对于字典,您可以执行for key, val in dict_like)

Jade uses what I refer to as "implicit enumeration" - it enumerates values in a list simply by adding one more variable, i, than there are values to unpack: for item, i in list_like (For dicts you can do for key, val in dict_like)

下面显示的是您使用元组拆包和隐式枚举"的示例,并通过PyJade 2.0.2进行了测试

Shown below is your example using tuple unpacking and "implicit enumeration" together, tested with PyJade 2.0.2

- var selected_index = 0
- var tabs = [('hello', '/world'), ('citizens', '/please/respect_your_mother'), ('thank_you', '/bye')]
ul
    // unpack `tabs` and tack on the variable `i` to hold the current idx
    for label, link, i in tabs
        li(class="selected" if (i == selected_index) else "")
            a(href="#{link}") #{label}

注意:截至撰写本文时,在标准" Jade代码中更常见的是, PyJade不支持赋值三元运算符. (variable= (condition)? value_if_true : value_if_false)

NOTE: As more commonly seen in "standard" Jade code, as of this writing, PyJade does NOT support the ternary operator for assignment. (variable= (condition)? value_if_true : value_if_false)

这篇关于在pyjade中使用python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆