jinja2 print to console or logging

A similar but slightly different approach using context processor:

In python / flask:

@app.context_processor
def utility_functions():
    def print_in_console(message):
        print str(message)

    return dict(mdebug=print_in_console)

In jinja2, Use it anywhere as follows:

{{ mdebug("any text or variable") }}

I think you can achieve it using filters (http://jinja.pocoo.org/docs/api/#custom-filters) or extensions (http://jinja.pocoo.org/docs/extensions/#adding-extensions). The idea is to just print the filter or extension straight to console.

Not tested but the filter should be something like:

def debug(text):
  print text
  return ''

environment.filters['debug']=debug

To be used as:

...<p>Hello world!</p> {{"debug text!"|debug}}...

Remember to remove the debug on production code!