答案 0 :(得分:1)
您肯定有此枝条扩展的服务定义,例如:
<service id="test_post_extension" class="Test\PostBundle\Twig\Extensions\TestPostExtension">
<tag name="twig.extension" />
</service>
您可以在twig.extension.trans
服务中注入任何其他服务。
xml变为:
<service id="test_post_extension" class="Test\PostBundle\Twig\Extensions\TestPostExtension">
<tag name="twig.extension" />
<argument type="service" id="twig.extension.trans" />
</service>
然后,您的TestPostExtension类将接收Symfony\Bridge\Twig\Extension\TranslationExtension
作为其构造函数的第一个参数:
namespace Test\PostBundle\Twig\Extensions;
class TestPostExtension extends \Twig_Extension
{
public function __construct(Symfony\Bridge\Twig\Extension\TranslationExtension $transExt) {
$this->transExt = $transExt;
}
// some stuff
}