0
我需要在模板TWIG中調用PHP函數。如何和在哪裏插入該功能? 這裏我舉的例子: 我的控制器是:Symfony2和Twig:我如何以及在哪裏插入用於TWIG的PHP函數?
namespace BackendBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use BackendBundle\Entity\Comisiones;
use BackendBundle\Entity\Liquidaciones;
use BackendBundle\Form\ComisionesType;
use BackendBundle\Form\LiquidacionesType;
/**
* Comisiones controller.
*
*/
class ComisionesController extends Controller
{
/**
* Lists all Comisiones entities.
*
*/
public function indexAction()
{
$twig = new Twig_Environment($loader);
$function = new Twig_SimpleFunction("decir_hola", function() {
$saludo='Hola!';
return $saludo;
});
$em = $this->getDoctrine()->getManager();
$comisiones = $em->getRepository('BackendBundle:Comisiones')->findComisio();
return $this->render('comisiones/index.html.twig', array(
'comisiones' => $comisiones,
));
}
我的細枝模板是:
{% block body %}
{{decir_hola()}}
{% endblock %}
但我收到此錯誤信息:
嘗試從命名空間加載類 「Twig_Environment」 「BackendBundle \控制器」。 你忘記了另一個命名空間的「使用」語句嗎?
缺什麼?
非常感謝您的快速回復!我是Twig和Symfony2的初學者。我不知道那篇告訴我的文章。我會立即閱讀 –