Example - Calling JS functions

Calling JS function within a template is quite easy. Just call your JS function like is a method of an object in your template context. Before calling the template function add your JS function to the template context.

Velocity template

<HTML>
<BODY>
Hello $world.name!<br/>
Today is $world.showCurrentDate();
</BODY>
</HTML>

Generated Javascript

function helloworld(context) { 
	var text = new StringCat();
	text.push('<HTML><BODY>Hello ');
	text.push(context.world.name);
	text.push('<br/>');
	text.push(context.world.showCurrentDate());
	text.push('</BODY></HTML>');
	return text.toString();
}

Adding JS function to template context

	...
	// adding your js function to the template context
	context.world.showCurrentDate = function() {
		return new Date();
	}
	var html = v2js_helloworld(context);
	...

Test it