简体   繁体   中英

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 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM