BasePresenter and $this->template triggers Creation of dynamic property is deprecated
- Ajax
- Member | 59
Hello All!
Back to Nette after 5 years, I installed Nette 3.2 and PHP 8.3 and tried to fill layout variable (for nav) like few years ago. And, as suggested, I used template class for variables. But that caused the problem. Here is my code:
/**
* @property-read BaseTemplate $template
*/
class BasePresenter extends Nette\Application\UI\Presenter
{
public function beforeRender(): void {
parent::beforeRender();
$this->template->availableProjects = ['a', 'b', 'c'];
}
}
class BaseTemplate extends Nette\Bridges\ApplicationLatte\Template
{
public array $availableProjects;
}
/**
* @property-read HomeTemplate $template
*/
final class HomePresenter extends BasePresenter
{
public function __construct(private readonly \App\Model\ProjectFactory $projectFactory) {
}
public function renderDefault(string $slug): void {
$project = $this->projectFactory->create($slug);
$this->template->project = $project;
}
}
class HomeTemplate extends Nette\Bridges\ApplicationLatte\Template
{
public \App\Model\Project $project;
}
home/default.latte
:
{templateType App\Presenters\HomeTemplate}
{block content}
Hello {$project->getName()}!
{/block}
@layout.latte
is standard, just on beginning, I added
{templateType App\Presenters\BaseTemplate}
This code triggers strange error:
Creation of dynamic property App\Presenters\HomeTemplate::$availableProjects is deprecated
.
Strange is, that he is looking for $availableProjects
variable in
HomeTemplate, but I'm using it it in BasePresenter and layout. When I comment
out line
$this->template->availableProjects = ['a', 'b', 'c'];
, error
is gone. Is there any way to fix it? Is there better way to share code for
layout in all presenters without adding variable in all template classes?
Thanks in advance!