Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python jinja2 examples
#!/usr/bin/env/python
#
# More of a reference of using jinaj2 without actual template files.
# This is great for a simple output transformation to standard out.
#
# Of course you will need to "sudo pip install jinja2" first!
#
# I like to refer to the following to remember how to use jinja2 :)
# http://jinja.pocoo.org/docs/templates/
#
from jinja2 import Environment
HTML = """
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
Hello.
</body>
</html>
"""
def print_html_doc():
print Environment().from_string(HTML).render(title='Hellow Gist from GutHub')
if __name__ == '__main__':
print_html_doc()
#!/usr/bin/env/python
#
# Using the file system load
#
# We now assume we have a file in the same dir as this one called
# test_template.html
#
from jinja2 import Environment, FileSystemLoader
import os
# Capture our current directory
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
def print_html_doc():
# Create the jinja2 environment.
# Notice the use of trim_blocks, which greatly helps control whitespace.
j2_env = Environment(loader=FileSystemLoader(THIS_DIR),
trim_blocks=True)
print j2_env.get_template('test_template.html').render(
title='Hellow Gist from GutHub'
)
if __name__ == '__main__':
print_html_doc()
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
Hello.
</body>
</html>
@xiconet

This comment has been minimized.

Copy link

xiconet commented Feb 25, 2014

Thanks for the tips

@nueverest

This comment has been minimized.

Copy link

nueverest commented Jan 23, 2016

This just saved me time.

@userlerueda

This comment has been minimized.

Copy link

userlerueda commented Jan 19, 2017

Isn't jinja2_file_system_loader.py missing import os?

@jakeingit

This comment has been minimized.

Copy link

jakeingit commented Mar 16, 2017

helped a lot, thanks

@JavaCS3

This comment has been minimized.

Copy link

JavaCS3 commented Mar 27, 2017

Thanks a lot

@dorukgezici

This comment has been minimized.

Copy link

dorukgezici commented May 26, 2017

Nice examples, thanks!

@tongshan1

This comment has been minimized.

Copy link

tongshan1 commented Aug 23, 2017

thanks!!!!!!!!!!!

@KFDCompiled

This comment has been minimized.

Copy link

KFDCompiled commented Nov 5, 2017

I too found this very helpful

@emlun007

This comment has been minimized.

Copy link

emlun007 commented Nov 24, 2017

thanks :)

@diepmv

This comment has been minimized.

Copy link

diepmv commented Dec 26, 2017

thank you

@baziliolg

This comment has been minimized.

Copy link

baziliolg commented Feb 17, 2018

Thank you!
Official documentation suggests I should build a rocket first before I can render the template.

@justin-thon

This comment has been minimized.

Copy link

justin-thon commented Mar 6, 2018

Thanks! Just what I needed.

Now I can migrate from flask to any newer frameworks without worrying about losing the helpful Jinja template :)

@cshilton

This comment has been minimized.

Copy link

cshilton commented Sep 6, 2018

Thanks alot!!

@deepanprabhu

This comment has been minimized.

Copy link

deepanprabhu commented Sep 7, 2018

Thanks,

@AlexDemian

This comment has been minimized.

Copy link

AlexDemian commented Oct 11, 2018

Thx, dude :)

@10000TB

This comment has been minimized.

Copy link

10000TB commented Feb 4, 2019

Simple yet very useful to get started with Jinja2 , THANK YOU!

@kys061

This comment has been minimized.

Copy link

kys061 commented Feb 11, 2019

Thank you

@SadBunny

This comment has been minimized.

Copy link

SadBunny commented Mar 8, 2019

If only all google results were this helpful!

@pangyuteng

This comment has been minimized.

Copy link

pangyuteng commented Jun 25, 2019

very useful. I have visited this gist for probably more than 5+ times now. thanks for sharing!

@pmtoan

This comment has been minimized.

Copy link

pmtoan commented Jan 9, 2020

Thank you. You saved my time!

@RedOctober45

This comment has been minimized.

Copy link

RedOctober45 commented Jan 9, 2020

I'm just trying to create a simple & basic HTML template for a Python app and this was perfect, very simple and just what I was looking for!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.