Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getContainer in symfony

Tags:

php

symfony

I am trying to get a block from a twig template them render it to my index template:

{% block round1 %}
<h1> hello this is a sample for a round 1</h1>
{% endblock %}

{% block round2 %}
<h1> hello this is a sample for a round 1</h1>
{% endblock %}

then get it to my controller using

  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Alvin\MainBundle\Entity\User;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  use Alvin\MainBundle\Form\Type\ResetPasswordType;

 
  $templateContent = $this->getContainer()->get('twig')->loadTemplate('AngpaoMainBundle:Dynamic:dynamic.html.twig');
  $bodydynamics = $templateContent->renderBlock('round1');

then use it in my index template

{{dynamic}}

but then im having a problem and symfony says

FatalErrorException: Error: Call to undefined method 
Alvin\MainBundle\Controller\IndexController::getContainer() in /Users/alvinvaldez/Sites/alvinwebsite/src/Alvin/MainBundle/Controller/IndexController.php line 26

I dont know what to use to get the container running.

like image 741
Aoi Avatar asked Jan 11 '23 12:01

Aoi


1 Answers

There is no getContainer() method for a controller.

You could access it by $this->container directly like $this->container->get('twig')

But Symfony provide shortcut method for controller, you could use $this->get('twig') too.

like image 136
xdazz Avatar answered Jan 16 '23 22:01

xdazz