希望这是我的某种愚蠢的疏忽,但是我几乎没有运气找到关于这个或其他类似使用Laravel的例子的信息。我正在开发一个Laravel 4网站,它的内容不是由本地数据库填充的,而是通过Tumblr API从一个特定的Tumblr博客上发布的帖子。
每一个Tumblr帖子都有一个与之相关的特定类型("文本","视频","照片"等),每种类型都有完全不同类型的内容需要吐出来,所以我为每种帖子类型都有一个Blade模板,继承了主post
Blade模板。(现在一切都只是一个存根。)
要填写首页,在我的控制器中,我正在填充那些帖子视图($postViews
)的数组。令人恼火的是,如果我循环$postViews
并在控制器中回显每个单独的视图,它包含适当的内容——数组中的所有三个视图都显示在最终站点的正确模板中。
但是当我将$postViews
发送到我的welcome
视图,然后在THERE中循环$postViews
时,它只呈现数组的第一个视图的三个实例。我不知道为什么。
下面是相关代码。正如您在欢迎模板中看到的那样,我尝试在欢迎视图中使用本地PHP和Laravel模板语法循环$postViews
。它们都表现出相同的行为:只显示三个帖子中的第一个,三次。
// controllers/HomeController.php
class HomeController extends BaseController {
public function showIndex()
{
$client = new Tumblr'API'Client(CONSUMERKEY, CONSUMERSECRET);
$tumblrData = (array) ($client->getBlogPosts(BLOGNAME));
$postViews = array();
foreach ($tumblrData['posts'] as $post) {
$post = (array) $post;
$type = TumblrParse::getPostType($post);
$postViews[] = View::make('tumblr.'.$type, array('post' => $post));
}
foreach ($postViews as $p){
echo $p;
// This works! It displays each post view properly before
// before rendering the welcome view, but I need them to
// be inside the welcome view in a specific place.
}
return View::make('home.welcome')->with('postViews', $postViews);
}
// views/home/welcome.blade.php
@extends('layouts.master')
@section('title')
@parent :: Welcome
@stop
@section('content')
<h1>Hello World!</h1>
<?php
foreach($postViews as $p) {
echo $p; // Just outputs the first of the array three times
}
?>
@foreach($postViews as $p)
{{ $p }} // Just outputs the first of the array three times
@endforeach
@stop
// views/layouts/post.blade.php
<div class="post">
@yield('postcontent')
</div>
// views/tumblr/photo.blade.php
// Other post types have their own views: video.blade.php, text.blade.php, etc.
@extends('layouts.post')
@section('postcontent')
<h1>This is a photo post!</h1>
<?php var_dump($post); ?>
@stop
我真的很感谢任何帮助!我是Laravel的新手,这一点我相信是显而易见的。据我所知,我在PHP中做错了一些事情,而不是在Laravel中。
在这种情况下,在使用视图渲染方法将每个视图附加到$postViews数组之前,将其渲染为字符串可能是有意义的。
$postViews[] = View::make('tumblr.'.$type, array('post' => $post))->render();
嵌套视图呢?检查文档。