如何将 php 变量(不是来自实体 class)调用到树枝文件中

How to call a php variable (not from a entity class) into a twig file

我想调用一个 php 变量到一个 twig 文件中。

假设在我的函数中我得到“$usrname”,那么我如何return twig 文件中的值---

public function helloTestAction(){
    $usrname = $user->getUserDetails()->get()->getLatestName();
    // UserDetails is the entity class name
.........
} 

假设我得到答案 "Hello Test",那么现在我如何 return 这个值到我的 twig 文件中。

那么如何在我的 twig 文件中获取“$usrname”。

任何人都知道这个问题的任何解决方案。提前致谢。

您可以如下使用:

return $this->render('AcmeDemoBundle:Welcome:index.html.twig', array(
            'usrname' => $usrname
    ));