Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4361ce683f
Fetching contributors…

Cannot retrieve contributors at this time

86 lines (79 sloc) 2.129 kb
<!doctype html>
<html>
<head>
<title>Kernel Template Language Test in Browser</title>
<script src="kernel.js"></script>
<script name="tasks" type="text/x-kernel-template">
<h1>Tasks for {name}</h1>
{#if(tasks.length)}
<ul>
{#loop(tasks)}
<li>{task} ({{index + 1}}/{tasks.length})</li>
{/loop}
</ul>
{/if}
{#unless(tasks.length)}
<p>Congratulations! Your tasks are all done</p>
{/unless}
{partial("footer", this)}
</script>
<script name="footer" type="text/x-kernel-template">
<footer>This was tasks for {name}</footer>
</script>
</head>
<body>
<div id="target"></div>
<script>
"use strict"
var helpers = {
partial: function (name, locals, callback) {
Kernel(name, function (err, template) {
if (err) {return callback(err); }
template(locals, callback);
});
},
if: function (condition, block, callback) {
if (condition) return block({}, callback);
callback(null, "");
},
unless: function (condition, block, callback) {
if (!condition) return block({}, callback);
callback(null, "");
},
loop: function (array, block, callback) {
var left = array.length;
if (!left) return callback(null, "");
var parts = new Array(left);
var done = false;
array.forEach(function (value, index) {
value.index = index;
block(value, function (err, result) {
parts[index] = result;
if (done || --left) return;
done = true;
if (err) return callback(err);
callback(null, parts.join(""));
});
});
}
}
var site = Object.create(helpers);
site.name = "Tim Caswell";
Kernel("tasks", function (err, template) {
if (err) throw err;
var data = Object.create(site);
data.tasks = [
{task: "Program Awesome Code"},
{task: "Play with Kids"},
{task: "Answer Emails"},
{task: "Write Blog Post"},
];
template(data, function (err, result) {
if (err) throw err;
if (err) throw err;
document.getElementById('target').innerHTML = result;
});
});
</script>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.