Skip to content

Instantly share code, notes, and snippets.

@dbrandt
Created May 15, 2013 07:41
Show Gist options
  • Save dbrandt/5582243 to your computer and use it in GitHub Desktop.
Save dbrandt/5582243 to your computer and use it in GitHub Desktop.
Simple way to test jinja templates. Depends on Flask (and by extension, Jinja2). Copy shell script to the directory where you have the templates you want to test run then execute script and visit localhost:5000/<template_name>
#!/bin/bash
/bin/chmod 0755 $0
if [ ! -f "jinja_test.py" ]; then
cat <<EOF > jinja_test.py
import os
from flask import Flask, abort, render_template
from jinja2 import FileSystemLoader
app = Flask(__name__)
app.jinja_loader = FileSystemLoader(
os.path.dirname(os.path.abspath(__file__)))
@app.route("/<tpl>")
def test_template(tpl=None):
if os.path.exists(tpl):
return render_template(tpl)
else:
abort(404)
app.run(debug=True)
EOF
fi
# source ~/bsedev/bin/activate # Uncomment/change if using virtualenv
/usr/bin/env python jinja_test.py
/bin/rm jinja_test.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment