Compiling templates

Adding template directories

First of all, Hawkejs needs to know where your templates are. You can add multiple directories using the Hawkejs#addViewDirectory method:

var hawkejs = new Hawkejs();

From then on it will look in those directories anytime a template is wanted. Paths are always interpreted as absolute, so starting with a forward slash in your templates is not required.

Compile example

Hawkejs will compile your templates into functions. These functions will create a virtual DOM, so it is important to write valid HTML (+ the allowed extra Hawkejs syntax)

Take this simple hawkejs template:

<% start('my-block'); %>
<div class="my-class">
	<% $0.setAttribute('id', 'test') %>
		This is regular text.
		Let's print a variable: {%= some_variable %}
<% end('my-block') %>

This will be compiled into a function that looks like this (formatting has been changed, some error catching methods have been removed):

function compiledView(__render, __template, vars, helper) {


		__render.printElement("div", {attributes: {"class": "my-class"}});
			__render.$0.setAttribute('id', 'test');

				__render.print("\n\t\tThis is regular text\n\t\tLet's print a variable: ");
				(__render.startExpression("Print", [{"variable":["some_variable"]}], vars).close());




As you can see, you can use both the EJS (<% %>) and Hawkejs ({% %}) syntax in the same template.