Skip to content

Instantly share code, notes, and snippets.

@alexmic
Last active December 31, 2015 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexmic/7946274 to your computer and use it in GitHub Desktop.
Save alexmic/7946274 to your computer and use it in GitHub Desktop.
Microtemplates example, read from JSON file.
{
"webpage": {
"title": "Stackoverflow"
},
"cover_artists": [
{
"name": "Ozzy",
"nicknames": ["Ozzman", "Ozzster"]
},
{
"name": "Slipknot",
"nicknames": ["Slip"]
}
],
"songs": {"name": "Mr Crowley"}
}
import json
from microtemplates import Template
if __name__ == '__main__':
with open('data.json') as f:
context = json.loads(f.read())
with open('template.html') as f:
html = f.read()
template = Template(html)
with open('rendered_template.html', 'w') as f:
rendered = template.render(**context)
f.write(rendered)
<html>
<head>
<title>Stackoverflow></title>
</head>
<body>
<h1>Mr Crowley></h1>
<h2>Ozzy</h2>
<p>Ozzman</p>
<p>Ozzster</p>
<h2>Slipknot</h2>
<p>Slip</p>
</body>
</html>
<html>
<head>
<title>{{ webpage.title }}></title>
</head>
<body>
<h1>{{ songs.name }}></h1>
{% each cover_artists %}
<h2>{{ it.name }}</h2>
{% each it.nicknames %}
<p>{{ it }}</p>
{% end %}
{% end %}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment