Fixes #839 - When rendering partials from component PHP code, be firm about the context

This commit is contained in:
Samuel Georges 2015-01-03 15:15:57 +11:00
parent ae8289735c
commit df3e66246f
2 changed files with 32 additions and 6 deletions

View File

@ -168,6 +168,16 @@ abstract class ComponentBase extends Extendable
return $this->alias;
}
/**
* Renders a requested partial in context of this component,
* see Cms\Classes\Controller@renderPartial for usage.
*/
public function renderPartial()
{
$this->controller->setComponentContext($this);
return call_user_func_array([$this->controller, 'renderPartial'], func_get_args());
}
/**
* Executes the event cycle when running an AJAX handler.
* @return boolean Returns true if the handler was found. Returns false otherwise.

View File

@ -84,11 +84,6 @@ class Controller extends BaseController
*/
protected $pageContents;
/**
* @var string Alias name of an executing component.
*/
protected $componentContext;
/**
* @var array A list of variables to pass to the page.
*/
@ -99,8 +94,19 @@ class Controller extends BaseController
*/
protected $statusCode = 200;
/**
* @var self Cache of self
*/
protected static $instance = null;
/**
* @var Cms\Classes\ComponentBase Object of the active component, used internally.
*/
protected $componentContext;
/**
* @var array Component partial stack, used internally.
*/
protected $partialComponentStack = [];
/**
@ -701,7 +707,7 @@ class Controller extends BaseController
}
elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) {
if ($throwException) {
throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name]));
throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$partialName]));
}
else {
return false;
@ -1095,6 +1101,16 @@ class Controller extends BaseController
return null;
}
/**
* Set the component context manually, used by Components when calling renderPartial.
* @param ComponentBase $component
* @return void
*/
public function setComponentContext(ComponentBase $component)
{
$this->componentContext = $component;
}
/**
* Sets component property values from partial parameters.
* The property values should be defined as {{ param }}.