package pier.util { public class Template { //RegExp to find #{variable_name} private var _pattern:RegExp = new RegExp("(^|.|\r|\n)(#\{(.*?)\})", "im"); private var _template:String; private var _template_original:String; //_obj is the Object hash containing the data to insert into the report string private var _obj:Object; public function Template(template:String):void { _template_original = template; } public function evaluate(obj:Object):String { //do this so you don't overwrite the original template :: subsequent calls would fail otherwise _template = new String(_template_original); //start the actual evaluation loop var result:String = doEvaluate(obj); return result; } private function doEvaluate(obj:Object):String { _obj = obj; //replace the match found in the report with the hash property _template = _template.replace(_pattern, insertTxt); //if another match was found continue the loop if(_template.match(_pattern)){ _template = this.doEvaluate(_obj); } _obj = null; return _template; } private function insertTxt():String { //third argument is the variable located in the hash if(_obj[arguments[3]]) { return _obj[arguments[3]]; } else { return ""; } } } }