1

I'm using flask with pyjade for templating. I can access context variables within templates directly, and even in inherited templates, but not when I include mixins or try to import mixins from another template. Here's the example:

My Flask application serves this page:

def home():
    return render_template('index.jade', foo='bar')

So, foo is in the context now. Now let's say I have the following mixin in mixins.jade:

mixin m()
    div= foo

And in my template index.jade:

- from 'mixins.jade' import m

div= foo
+m()

In the template itself, I can read foo just fine, but in the imported mixin, I get

jinja2.exceptions.UndefinedError: 'foo' is undefined

Is this a shortcoming of pyjade, or is there a workaround?

maackle
  • 2,074
  • 2
  • 15
  • 27

1 Answers1

1

this is not caused by pyjade but Jinja2...

You have to import the template "with context". See Jinja2 docs

This should work:

- from 'mixins.jade' import m with context

div= foo
+m()
Syrus Akbary Nieto
  • 1,249
  • 12
  • 20
  • 1
    Thank you, and thanks for pyjade! While I've got you here, shouldn't `include mixins` work here as well to include all mixins in the file, without having to explicitly import mixins with `from _ import _`? Because it doesn't... – maackle Sep 26 '14 at 22:13
  • It should, but that goal was never reached before the repository turned inactive: https://github.com/syrusakbary/pyjade/issues/70 – Magnus Lind Oxlund Feb 03 '18 at 23:26