Skip to content

Instantly share code, notes, and snippets.

@ycatch
Last active March 17, 2018 14:42
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 ycatch/85e89afed9ad4a6856836a9017b7c70b to your computer and use it in GitHub Desktop.
Save ycatch/85e89afed9ad4a6856836a9017b7c70b to your computer and use it in GitHub Desktop.
Handlebars.js: Minimal Templating on Steroids https://handlebarsjs.com/
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Template Test</title>
<style>
body {
padding: 30px;
}
</style>
<meta charset="utf-8">
<meta name="description" content="Test for lightweight template engine">
<meta name="author" content="Yutaka Kachi">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.min.js"></script>
<script type="text/x-handlebars-template" id="template">
<h1>{{name}}</h1>
<div>{{description}}</div>
<div>{{date}}</div>
</script>
<script>
window.onload = function() {
var source = $("#template").html();
var template = Handlebars.compile(source);
var values = {
name : "Hello World",
description : "吾輩は猫である。",
date: function() {
return new Date();
}
};
var html = template(values);
console.log(html);
$("#contents").html(html);
}
</script>
</head>
<body id="contents"></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment