[英]getContainer in symfony
我正在嘗試從樹枝模板中獲取一個塊,然后將其渲染到我的索引模板中:
{% 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 %}
然后使用它到我的控制器
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');
然后在我的索引模板中使用它
{{dynamic}}
但后來我遇到了問題,symfony 說
FatalErrorException: Error: Call to undefined method
Alvin\MainBundle\Controller\IndexController::getContainer() in /Users/alvinvaldez/Sites/alvinwebsite/src/Alvin/MainBundle/Controller/IndexController.php line 26
我不知道用什么來運行容器。
控制器沒有getContainer()
方法。
你可以像$this->container->get('twig')
一樣直接通過$this->container
訪問它
但是 Symfony 為控制器提供了快捷方法,您也可以使用$this->get('twig')
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.