DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple Templating Engine

08.04.2005
| 1607 views |
  • submit to reddit
        this will replace variables within a string that are denoted as [[variable]] and will replace it with the value of data[variable]


function parseTemplate(content, data) {
	content=String(content);
	re = /\[\[(\w*)\]\]/gi;

	return (
		content.replace(
			re, 
			function ($1,$2,$3) {
				return data[$2];
			}
		)
	);
}