public function instantiateIn($tplControl, $parentControl = null)
{
$this->_tplControl = $tplControl;
if ($parentControl === null) {
$parentControl = $tplControl;
}
if (($page = $tplControl->getPage()) === null) {
$page = $this->getService()->getRequestedPage();
}
$controls = array();
$directChildren = array();
foreach ($this->_tpl as $key => $object) {
if ($object[0] === -1) {
$parent = $parentControl;
} else {
if (isset($controls[$object[0]])) {
$parent = $controls[$object[0]];
} else {
continue;
}
}
if (isset($object[2])) {
$component = Prado::createComponent($object[1]);
$properties =& $object[2];
if ($component instanceof TControl) {
if ($component instanceof \Prado\Web\UI\WebControls\TOutputCache) {
$component->setCacheKeyPrefix($this->_hashCode . $key);
}
$component->setTemplateControl($tplControl);
if (isset($properties['id'])) {
if (is_array($properties['id'])) {
$properties['id'] = $component->evaluateExpression($properties['id'][1]);
}
$tplControl->registerObject($properties['id'], $component);
}
if (isset($properties['skinid'])) {
if (is_array($properties['skinid'])) {
$component->setSkinID($component->evaluateExpression($properties['skinid'][1]));
} else {
$component->setSkinID($properties['skinid']);
}
unset($properties['skinid']);
}
$component->trackViewState(false);
$component->applyStyleSheetSkin($page);
foreach ($properties as $name => $value) {
$this->configureControl($component, $name, $value);
}
$component->trackViewState(true);
if ($parent === $parentControl) {
$directChildren[] = $component;
} else {
$component->createdOnTemplate($parent);
}
if ($component->getAllowChildControls()) {
$controls[$key] = $component;
}
} else {
if ($component instanceof TComponent) {
$controls[$key] = $component;
if (isset($properties['id'])) {
if (is_array($properties['id'])) {
$properties['id'] = $component->evaluateExpression($properties['id'][1]);
}
$tplControl->registerObject($properties['id'], $component);
if (!$component->hasProperty('id')) {
unset($properties['id']);
}
}
foreach ($properties as $name => $value) {
$this->configureComponent($component, $name, $value);
}
if ($parent === $parentControl) {
$directChildren[] = $component;
} else {
$component->createdOnTemplate($parent);
}
}
}
} else {
if ($object[1] instanceof TCompositeLiteral) {
// need to clone a new object because the one in template is reused
$o = clone $object[1];
$o->setContainer($tplControl);
if ($parent === $parentControl) {
$directChildren[] = $o;
} else {
$parent->addParsedObject($o);
}
} else {
if ($parent === $parentControl) {
$directChildren[] = $object[1];
} else {
$parent->addParsedObject($object[1]);
}
}
}
}
// delay setting parent till now because the parent may cause
// the child to do lifecycle catchup which may cause problem
// if the child needs its own child controls.
foreach ($directChildren as $control) {
if ($control instanceof TComponent) {
$control->createdOnTemplate($parentControl);
} else {
$parentControl->addParsedObject($control);
}
}
}