Reputation: 8707
I have TheParentController
and the inheritating TheChildController
, which should assign $moreData
to the template, but the render()
method should be called in TheParentController
.
Is there any function/service for this case? I expect anything like
$this->get('templating')->assignDataForTemplate('moreData', $moreData);
class TheParentController
{
public function myAction($param1) {
return $this->render('template.html.twig', array(
'someData' => $someData
));
}
}
-
class TheChildController
{
public function myAction($param1) {
// !
// Is there any function like "assignDataForTemplate"?
$this->get('templating')->assignDataForTemplate('moreData', $moreData);
// /!
return parent::myAction($param1);
}
}
I would like to avoid something like
// ...
public function myAction($param1, $moreData = null) {
return $this->render('template.html.twig', array(
'someData' => $someData,
'moreData' => $moreData
));
}
}
Upvotes: 1
Views: 64
Reputation: 21
You could try something like this, so that the parent is unaware of the child.
<?php
class TheParentController {
public function myAction () {
$data = $this->getMyActionData();
return $this->render('template', $data);
}
protected function getMyActionData () {
return [
'someDefault' => 5
];
}
}
class TheChildController extends TheParentController {
// If using annotation based routing override myAction
// with call to parent function and new @Route tag in doc block
protected function getMyActionData () {
$parentData = parent::getMyActionData();
return array_merge($parentData, [
'childData' => 11
]);
}
}
Upvotes: 1
Reputation: 17042
As far as I'm aware, there is no such way to do this currently. If you go through the sources, you'll see that calling $templating->render()
is actually calling TwigEngine->render()
.That calls Twig_Template->render()
which outputs the template to the client.
I fully understand why you might be using HMVC but I believe this approach might be overcomplicating things for you. If you have common code between controllers - just create a static class which can be called directly. Afterwards move your common logic/code there and call it whenever you need it.
Otherwise, you might need to stick with the code you're trying to avoid (or similar workaround) for now.
Upvotes: 2