Last active
August 29, 2015 14:16
-
-
Save dirkk0/3eee5d321121e716ccab to your computer and use it in GitHub Desktop.
Using the jsontemplate engine (or, the beauty of python)
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
# via http://json-template.googlecode.com/svn/trunk/doc/Introducing-JSON-Template.html | |
import jsontemplate | |
import json | |
# load template | |
template = open('test_template.html', 'r').read() | |
# load data | |
data = json.loads(open('test_data.json', 'r').read()) | |
# use jsontemplate | |
result = jsontemplate.expand(template, data) | |
# write file | |
url = 'out.html' | |
open(url, 'w').write(result) | |
# open in browser | |
import webbrowser | |
chrome_path = 'open -a /Applications/Google\ Chrome.app %s' | |
webbrowser.get(chrome_path).open(url) |
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
{ | |
"url-base": "http://example.com/music/", | |
"playlist-name": "Epic Playlist", | |
"songs": [ | |
{ | |
"url": "1.mp3", | |
"artist": "Grayceon", | |
"title": "Sounds Like Thunder" | |
}, | |
{ | |
"url": "2.mp3", | |
"artist": "Thou", | |
"title": "Their Hooves Carve Craters in the Earth" | |
} | |
] | |
} |
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
{# This is a comment and will be removed from the output.} | |
{.section songs} | |
<h2>Songs in '{playlist-name}'</h2> | |
<table width="100%"> | |
{.repeated section @} | |
<tr> | |
<td><a href="{url-base|htmltag}{url|htmltag}">Play</a> | |
<td><i>{title}</i></td> | |
<td>{artist}</td> | |
</tr> | |
{.end} | |
</table> | |
{.or} | |
<p><em>(No page content matches)</em></p> | |
{.end} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment