Last active
December 31, 2015 06:18
-
-
Save alexmic/7946274 to your computer and use it in GitHub Desktop.
Microtemplates example, read from JSON file.
This file contains 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
{ | |
"webpage": { | |
"title": "Stackoverflow" | |
}, | |
"cover_artists": [ | |
{ | |
"name": "Ozzy", | |
"nicknames": ["Ozzman", "Ozzster"] | |
}, | |
{ | |
"name": "Slipknot", | |
"nicknames": ["Slip"] | |
} | |
], | |
"songs": {"name": "Mr Crowley"} | |
} |
This file contains 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
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) |
This file contains 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
<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> |
This file contains 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
<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