Created
February 7, 2014 04:18
-
-
Save Beyamor/8857322 to your computer and use it in GitHub Desktop.
Jinja2 Templating Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using Jinja2 as the templating engine, | |
# this reads and renders some templates named "main-page.template" and "about-me.template", | |
# outputting "main-page.html" and "about-me.html" respectively. | |
# (you could call the files whatever you wanted) | |
# | |
# Importantly, each HTML template includes another file, "header.html". | |
# | |
# You could run the script with `python compile.py`. | |
# main-page.html | |
# | |
# <html> | |
# <body> | |
# {% include "header.html" %} | |
# <h2>Main Page<h2> | |
# <p>This page is rad!</p> | |
# </body> | |
# </html> | |
# about-me.template | |
# | |
# <html> | |
# <body> | |
# {% include "header.html" %} | |
# <h2>About Me<h2> | |
# <p>I'm great!</p> | |
# </body> | |
# </html> | |
# header.html | |
# | |
# <h1>Header</h1> | |
from jinja2 import Environment, FileSystemLoader, Template | |
env = Environment(loader=FileSystemLoader(".")) | |
for page_name in ["main-page", "about-me"]: | |
page = env.get_template(page_name + ".template").render() | |
with open(page_name + ".html", "w") as page_file: | |
page_file.write(page) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment