4

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

But I see this error:

UndefinedError: 'enumerate' is undefined

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

Anthon
  • 69,918
  • 32
  • 186
  • 246
Chung Wu
  • 2,357
  • 3
  • 21
  • 19
  • Looking at [the docs for pyjade](https://github.com/SyrusAkbary/pyjade), I don't see an example where Python can be directly embedded... because it seems like pyjade mostly acts as some sort of wrapper or parser for Jade Template Syntax. Is it possible to just try using a `for` or `each` loop [as described in the Jade docs](http://naltatis.github.com/jade-syntax-docs/#for)? – summea Apr 03 '13 at 05:03
  • Oh! Coming from scalade, I'd thought pyjade allows you to embed python code as scalade allows you to embed scala code. Does pyjade actually support arbitrary javascript expressions? Or does it only pick out a few to convert into Python template equivalent? – Chung Wu Apr 03 '13 at 17:09
  • Looking at [one of the examples in the pyjade docs](https://github.com/SyrusAkbary/pyjade#example) it looks like you can embed JavaScript in the [usual Jade Template Syntax way](http://jade-lang.com/); I don't think you have to worry about the Python side for JavaScript in this case. – summea Apr 03 '13 at 21:43
  • Which target language are you compiling to? Also, it seems the problem is that enumerate is not in the templating environment. – xiaq Apr 23 '13 at 03:33

4 Answers4

4

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)

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}

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)

Brian
  • 555
  • 4
  • 18
1

No; pyjade does not allow embedding arbitrary python code into jade. Use jade's syntax instead.

Chung Wu
  • 2,357
  • 3
  • 21
  • 19
1

You should use the way of adding functions to the templating environment that is used by the tamplate language you compile the jade files to using pyjade.

For Flask using jinja this should be put in your __init__.py would be:

app.jinja_env.globals.update(enumerate=enumerate)
JelteF
  • 3,021
  • 2
  • 27
  • 35
1

you can do that with pypugjs (successor of pyjade)

li(class=("selected" if i == selected_index else ""))
Andy
  • 726
  • 2
  • 6
  • 15