希望这是我的某种愚蠢的疏忽,但我很难找到有关此或 Laravel 类似用法的其他示例的信息。我正在开发一个 Laravel 4 站点,其内容不是使用本地数据库填充,而是通过 Tumblr API 使用来自特定 Tumblr 博客的帖子。
每个 Tumblr 帖子都有与之关联的特定类型(“文本”、“视频”、“照片”等),每种类型都有完全不同类型的内容需要吐出,所以我有一个 Blade 模板每个帖子类型,都继承自一个主post
Blade 模板。(现在一切都只是一个存根。)
为了填写首页,在我的控制器中,我用这些帖子视图 ( $postViews
) 填充了一个数组。令人抓狂的是,如果我循环并回显控制器中的$postViews
每个单独视图,它包含正确的内容——数组中的所有三个视图都显示在其正确模板内的最终站点上。
但是当我发送$postViews
到我的welcome
视图,然后$postViews
在 THERE 内部循环时,它只呈现数组的第一个视图的三个实例。我不知道为什么。
这是相关的代码。正如您在欢迎模板中看到的那样,我尝试$postViews
使用原生 PHP 和 Laravel 模板语法在欢迎视图中循环。它们都表现出相同的行为:只显示三个帖子中的第一个,三次。
// 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 中做错了。