[英]Laravel array in mustache template
This is my Controller: 这是我的控制器:
class HomeController extends BaseController {
public $layout = 'default';
public function index()
{
$page = Page::find(1);
$this->layout->page_title = $page->page_title;
$allPages = Page::lists('title');
// Give the page it's needed content
$this->layout->nest('content', 'pages.home', array(
'pageHeading' => 'Rendered with Mustache.php',
'pageContent' => $allPages
));
}
}
I have this master template called default.blade.php
and a child page template called home.mustashe
. 我有一个名为
default.blade.php
主模板和一个名为home.mustashe
的子页面模板。
Problem is, I want to use the array containing all my page titles in the home.mustache
template. 问题是,我想在
home.mustache
模板中使用包含所有页面标题的home.mustache
。 how do add my array into the home.mustache
template? 如何将我的数组添加到
home.mustache
模板中?
The code as it is now gives me following error: Array to string conversion
现在的代码给了我以下错误:
Array to string conversion
The Mustache Package i use is: Link to mustache package 我使用的小胡子软件包是: 链接到小胡子软件包
The docs on your Mustache package say arrays get handled as follows: Mustache软件包上的文档说,数组的处理方式如下:
{{#posts}}
{{> posts._post}}
{{/posts}}
That error means, quite literally, "You're trying to display an array as a string" ... which you haven't provided enough context do debug completely, but I'd guess means you're setting pageContent
as an array of values, then calling {{ pageContent }}
in a template. 从字面上看,该错误意味着“您正在尝试将数组显示为字符串” ...您没有提供足够的上下文来进行完全调试,但是我想这意味着您正在将
pageContent
设置为以下内容的数组值,然后在模板中调用{{ pageContent }}
。
Either convert your array to a string yourself: 自己将数组转换为字符串:
implode(', ', $allPages)
… or do something array-like with pageContent
inside the template: …或在模板中对
pageContent
进行类似数组的操作:
{{# pageContent }}<li>{{ . }}</li>{{/ pageContent }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.