1

I'm diving into Jade for the first time using PyJade so that I can use it within Flask (and Jinja2). So far everything seems to be working and compiling correctly, though I'm wondering if this is a bug in PyJade's translation?

I'm unable to inject ternary syntax into Jade without an error. I've tried it every way suggested here.

div(class=(form.name.errors.length===0 ? 'form-group' : 'form-group has-error'))
div(class="#{form.name.errors.length===0 ? 'form-group' : 'form-group has-error'}")

My error is always

TemplateSyntaxError: unexpected '='

or

TemplateSyntaxError: unexpected char u'?' at 693

Am I doing something wrong here?

Here's a simplified code snippet

doctype html
html(lang="en")
    head
        title= AwesomePage

    body

        form.form-horizontal(method="POST", action="/timeline")

            div(class="#{form.name.errors.length===0 ? 'form-group' : 'form-group has-error'}")
                div.col-sm-4
                    #{form.name(class="form-control")}

            div(class="col-sm-offset-2 col-sm-4")
                input(type="submit", class="btn btn-default", value="Add Vendor")
Community
  • 1
  • 1
Scott
  • 3,204
  • 3
  • 31
  • 41

1 Answers1

3

As pyjade is rendered with python syntax... yo have to follow python syntax, so this expression will succeed in js (form.name.errors.length===0 ? 'form-group' : 'form-group has-error')) but not in python, as the ternary operation in python is a if test else b.

So, the solution is:

div(class=('form-group' if not len(form.name.errors) else 'form-group has-error'))

Syrus Akbary Nieto
  • 1,249
  • 12
  • 20
  • Wow, that's awesome. I didn't realize the python element of this formula made it's way into the templates. So cool and such a great project. Thank you, Syrus! – Scott Oct 28 '14 at 21:10