/**
 * Template.js
 * 
 * Ported from Prototype libraries to be compatible with mootools 1.2b
 * 
 * @author Mark Story
 */

/**
 * Easy peasy Templating for Dynamically Added Elements
 * 
 */
var Template = new Class({
	_template : null,
	_data : {},
	/**
	 * Default Syntax matches {{ tagName }}
	 */	
	pattern : /\\?\{\{([^}]+)\}\}/g,
	
	/**
	 * Build the Template 
	 * @param string templateString  The template to be used later on.
	 * @param string  pattern  Regular Expression to change the template tags of the Template
	 */
	initialize : function(templateString, pattern) {
		this._template = templateString.get('html');
		this.pattern = pattern || this.pattern;
	},
	/**
	 * Evaluate a Template and replace the template tags with the data in the 
	 * Data object.
	 * 
	 * @param {Object} dataset Data that will fill the template out.
	 * @return string Template with datavalues replaced.
	 */
	evaluate : function(dataset) {
		return this._template.replace(this.pattern, function(match, name){
			if (match.charAt(0) == '\\') return match.slice(1);
			return (dataset[name] != undefined) ? dataset[name] : '';
		});
	}
	
});

