Skip to content
Advertisement

getContainer in 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 SymfonyBundleFrameworkBundleControllerController;
  use AlvinMainBundleEntityUser;
  use SymfonyComponentHttpFoundationRequest;
  use SymfonyBundleFrameworkBundleCommandContainerAwareCommand;
  use AlvinMainBundleFormTypeResetPasswordType;

 
  $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 
AlvinMainBundleControllerIndexController::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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement