Example - Hello World

This example transforms a velocity template which renders a greeting text in various languages.

Velocity template

<HTML>
<BODY>
Hello $world.name!
<table>
#foreach( $greeting in $greetings )
   <tr>
	<td>$greeting.language</td>
	<td>$greeting.text</td>
   </tr>
#end
</table>
</BODY>
</HTML>

Generated Javascript

function helloworld(context) { 
	var text = new StringCat();
	text.push('<HTML><BODY>Hello ');
	text.push(context.world.name);
	text.push('<table>');
	for (var i1=0;  i1<context.greetings.length; i1++) {
		var greeting = context.greetings[i1]);
		text.push('   <tr>	<td>');
		text.push(greeting.language);
		text.push('</td><td>');
		text.push(greeting.text);
		text.push('</td>	<td>');		
	}
	text.push('</table></BODY></HTML>');
	return text.toString();
}

Function call could look like this

var htmlstring = v2js_helloworld({ world: { name : 'WORLD' }, 
			greetings: [ { language : 'English', text: 'Hi' },
						 { language : 'German', text: 'Hallo' },
						 { language : 'Swedish', text: 'Hey' },
						 { language : 'Spanish', text: 'Ola' }
						] });
orderDiv.innerHTML = htmlstring;

Test it