簡體   English   中英

使用Doctrine實體模型渲染Mustache PHP模板

[英]Render Mustache PHP template with Doctrine entity model

在我的Symfony控制器中,我想呈現一個帶有Doctrine實體的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>

但是變量未放入模板中。 它僅顯示“用戶名:”字符串。 我也嘗試了沒有關聯數組:

$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模板。

編輯

當我閱讀該手冊時 ,應該可以在模型中提供一個對象:

在此處輸入圖片說明

如果您的用戶屬性不是公開的,它將無法正常工作。

嘗試{{ user.getName }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM