var TemplateFeature = require('./index.js'),
	XML = require('xmldom'),
	fs = require('fs');


/*
 *  Embed external templates
 *  @name    TemplateFeatureInclude
 *  @package Scaffold
 */
function TemplateFeatureInclude(element, template)
{
	var include = this;

	TemplateFeature.apply(include, arguments);


	/**
	 *  Replace the feature node with the contents of included file
	 *  @name    prepare
	 *  @type    method
	 *  @access  public
	 *  @return  void
	 */
	include.prepare = function(done)
	{
		var file = include.attribute('file'),
			dom;

		if (file)
		{
			file = fs.readFileSync(file);
			dom  = new XML.DOMParser().parseFromString('<include>' + file.toString() + '</include>');
			while (dom.documentElement.firstChild)
			{
				element.parentNode.insertBefore(element.ownerDocument.importNode(dom.documentElement.firstChild, true), element);
				dom.documentElement.removeChild(dom.documentElement.firstChild);
			}
		}
		include.clean();

		if (done)
			done(null, include);
	};
}

module.exports = TemplateFeatureInclude;

@@ -539,18 +539,29 @@
 le')
-,%0A%09%09%09dom;%0A
+;%0A%0A%09%09include.clean();
 %0A%09%09i
@@ -576,23 +576,16 @@
 %0A%09%09%7B%0A%09%09%09
-file = 
 fs.readF
@@ -591,27 +591,90 @@
 File
-Sync
 (file
-);%0A%09%09%09dom 
+, function(error, data)%7B%0A%09%09%09%09var dom;%0A%0A%09%09%09%09if (!error && data)%0A%09%09%09%09%7B%0A%09%09%09%09%09dom
  = n
@@ -726,23 +726,12 @@
 ' + 
-file.toString()
+data
  + '
@@ -744,16 +744,18 @@
 ude%3E');%0A
+%09%09
 %09%09%09while
@@ -788,16 +788,18 @@
 tChild)%0A
+%09%09
 %09%09%09%7B%0A%09%09%09
@@ -799,16 +799,18 @@
 %09%09%7B%0A%09%09%09%09
+%09%09
 element.
@@ -919,16 +919,18 @@
 t);%0A%09%09%09%09
+%09%09
 dom.docu
@@ -993,67 +993,150 @@
 %0A%09%09%09
-%7D%0A%09%09%7D%0A%09%09include.clean();%0A%0A%09%09if (done)%0A%09%09%09done(null, include
+%09%09%7D%0A%09%09%09%09%7D%0A%0A%09%09%09%09if (done)%0A%09%09%09%09%09done(error, include);%0A%09%09%09%7D);%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09done(new Error('no template file provided for k:include feature')
 );%0A%09

Include features are now processed asynchronously