Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用条令实体模型呈现Mustache PHP模板_Php_Symfony_Doctrine Orm_Mustache_Mustache.php - Fatal编程技术网

使用条令实体模型呈现Mustache PHP模板

使用条令实体模型呈现Mustache PHP模板,php,symfony,doctrine-orm,mustache,mustache.php,Php,Symfony,Doctrine Orm,Mustache,Mustache.php,在我的Symfony控制器中,我希望呈现一个带有条令实体的Mustache PHP模板: // The Doctrine entity, a dump($user) shows the correct entity $user = $this->get('x_service')->getUserById($id); $templateFile = file_get_contents('.../path/to/file'); // tested, it works $mustach

在我的Symfony控制器中,我希望呈现一个带有条令实体的Mustache PHP模板:

// The Doctrine entity, a dump($user) shows the correct entity
$user = $this->get('x_service')->getUserById($id); 

$templateFile = file_get_contents('.../path/to/file'); // tested, it works
$mustache = new \Mustache_Engine();
$renderedTemplate = $mustache->render($templateFile, array('user' => $user));
模板文件如下所示:

<p>
    User name: {{ user.name }}
</p>

用户名:{{User.name}

但是变量并没有放入模板中。它只显示“用户名:”字符串。我也尝试过不使用关联数组:

$renderedTemplate = $mustache->render($templateFile, $user);

// the template file:

<p>
    User name: {{ name }}
</p>
$renderedTemplate=$mustache->render($templateFile,$user);
//模板文件:

用户名:{{name}

这也不管用

但是,当我将另一个变量与关联数组放在一起时,会显示:

$renderedTemplate = $mustache->render($templateFile, array('user' => $user, 'meh' => 'hem'));

// the template file:

<p>
    User name: {{ name }} <- still no output
    Meh: {{ meh }} <- this does give output
</p>
$renderedTemplate=$mustache->render($templateFile,array('user'=>$user,'meh'=>'hem'));
//模板文件:


用户名:{{name}}如果您的用户属性不是公共的,它将不起作用


试试
{{user.getName}

Mustache想要一个数组,被赋予一个
实体
。建议加入一个数组,per:用于编辑:如果您的用户属性不是
公共的
,它将不起作用。TWIG打电话给getter,我不知道mustache是否也这么做。请尝试执行此操作的
{{user.getName}
@LP154。我太习惯于私有类变量了,以至于我没有注意到。你能把这个作为答案吗?还有,在我使用Mustache之前,JS,这意味着实体被转换成Javascript中的JSON对象,你可以在其中执行“user.name”。