2017-08-22 20 views
0

在我的Symfony控制器我想呈现胡须PHP模板,一个主义实体:渲染主义实体模型小胡子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> 

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

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

// the template file: 

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

这也没有工作。

然而,当我把另一个变量与关联数组,这并不表明:

$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> 

一些详细信息:我只是想呈现在我的控制器这个小小胡子模板。对于控制器响应,我使用Twig。这是因为我只是重复使用前端Mustache.js模板。

编辑

当我读到the manual,它应该工作给予模型中的一个对象:

enter image description here

+0

小胡子想要一个数组,给出一个“实体”。建议保湿数组,每个:http://doctrine-orm.readthedocs.io/en/latest/reference/dql-doctrine-query-language.html#array-hydration – bishop

+0

对于你的编辑:它不会工作,如果你的用户属性不是“公共”的。 TWIG称之为吸气剂,我不知道小胡子是否也这样做。尝试'{{user.getName}}' – LP154

+0

@ LP154那就做了。我很习惯私人类变量,我没有注意到。你可以发布这个答案吗? – BigJ

回答

1

,如果你的用户属性是不公开它不会工作。

尝试{{ user.getName }}