Skip to content

Instantly share code, notes, and snippets.

@dirkk0
Last active August 29, 2015 14:16
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 dirkk0/3eee5d321121e716ccab to your computer and use it in GitHub Desktop.
Save dirkk0/3eee5d321121e716ccab to your computer and use it in GitHub Desktop.
Using the jsontemplate engine (or, the beauty of python)
# 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)
{
"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 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