Skip to content

Using Handlebars Templates

madprops edited this page Aug 2, 2017 · 4 revisions

test.html

<html>

	<head>

		<script src="handlebars.js"></script>
		<script src="msg.js"></script>
		<script src="test.js"></script>
		<script>

			window.addEventListener('load', function()
			{
				init();
			});

		</script>
		
	</head>

	<body>

		<script id="example-template" type="text/x-handlebars-template">
			<div class="example">
				<div class="example-title">
					{{title}}
				</div>
				<div class="example-body">
					{{body}}
				</div>
			</div>
		</script>

	</body>

</html>

test.js

var example_template;

function init()
{
	msg = Msg();
	compile_templates();	
}

function compile_templates()
{
	example_template = Handlebars.compile(document.getElementById('example-template').innerHTML);
}

function show_example(title, content)
{
	var c = {title: title, body: content};
	var html = example_template(c);
	msg.show(html);
}

Using it

show_example("My Title", "My Body");
Clone this wiki locally