Created
August 19, 2009 03:34
-
-
Save edward/170141 to your computer and use it in GitHub Desktop.
Trying to figure out how to move the JSON template out to another 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
<html> | |
<head> | |
<script type="text/javascript" src="json-template.js"> | |
</script> | |
<script type="text/javascript"> | |
// Note that this requires json-template.js | |
function write() { | |
// Any ideas on how to move this template out to another file | |
// such that I don’t have to do the end-of-line string escaping? | |
var t = jsontemplate.Template( | |
"{# 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}"); | |
var s = t.expand({ | |
"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" | |
} | |
] | |
}); | |
document.getElementById("replace").innerHTML = s; | |
} | |
</script> | |
</head> | |
<body onload="write();"> | |
<h4>Here is the same example rendered in JavaScript. <b>View Source</b> to | |
see how it works.</h4> | |
<div id="replace"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment