Created
June 3, 2012 03:20
-
-
Save iggymacd/2861694 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Method will accept a Map, and populate a template | |
* Each child element (blocks and variables) will be processed as well | |
* as any <included> directives | |
* In case processing takes some significant time, we leverage a Future | |
*/ | |
Future<String> render(Map data){ | |
var completer = new Completer(); | |
String result = content; | |
List futuresPending = new List(); | |
try{ | |
data.forEach((String key, Dynamic value){ | |
if(value is List){ | |
String blockRenderResult = blocks[key].render(value); | |
result = result.replaceAll(blocks[key].sourceField, blockRenderResult); | |
}else if(includes.containsKey(key)){ | |
TemplateFactory tf = new TemplateFactory(); | |
Future<Template> futureTemplate = tf.compile('views/$key.template'); | |
futuresPending.add(futureTemplate); | |
futureTemplate.handleException(onException(exception){ | |
print('error occurred while processing include!'); | |
}); | |
futureTemplate.chain((Template templateFromCompile) => templateFromCompile.render(value)) | |
.transform(transformation(String returnedStringFromRender){ | |
result = result.replaceAll('{{>${includes[key].filename}}}', returnedStringFromRender.trim()); | |
}); | |
}else{ | |
result = result.replaceAll('{{${tags[key].name}}}', value); | |
} | |
}); | |
}catch(Exception e){ | |
completer.completeException(e); | |
} | |
Futures.wait(futuresPending).then(onComplete(List futureResults){ | |
completer.complete(result); | |
}); | |
return completer.future; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment