Php Laravel模板不在视图数组中循环

Php Laravel模板不在视图数组中循环,php,model-view-controller,laravel,tumblr,laravel-4,Php,Model View Controller,Laravel,Tumblr,Laravel 4,希望这是我的一种愚蠢的疏忽,但我几乎没有运气找到关于这个或其他类似使用Laravel的例子的信息。我正在开发一个Laravel4站点,它的内容不是用本地数据库填充的,而是通过TumblrAPI从特定Tumblr博客中发布的文章 每个Tumblr帖子都有一个与之相关的特定类型(“文本”、“视频”、“照片”等),每种类型都有完全不同的内容类型,需要吐出,因此我为每种帖子类型都有一个刀片模板,继承自主帖子刀片模板。(现在一切都只是一个存根。) 为了填写首页,在我的控制器中,我正在用这些post视图填充

希望这是我的一种愚蠢的疏忽,但我几乎没有运气找到关于这个或其他类似使用Laravel的例子的信息。我正在开发一个Laravel4站点,它的内容不是用本地数据库填充的,而是通过TumblrAPI从特定Tumblr博客中发布的文章

每个Tumblr帖子都有一个与之相关的特定类型(“文本”、“视频”、“照片”等),每种类型都有完全不同的内容类型,需要吐出,因此我为每种帖子类型都有一个刀片模板,继承自主
帖子
刀片模板。(现在一切都只是一个存根。)

为了填写首页,在我的控制器中,我正在用这些post视图填充一个数组(
$postViews
)。令人恼火的是,如果我循环通过
$postViews
并回显控制器中的每个视图,它包含正确的内容——数组中的所有三个视图都显示在其正确模板中的最终站点上

但是,当我将
$postViews
发送到我的
welcome
视图,然后在其中循环
$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
//controllers/HomeController.php
类HomeController扩展了BaseController{
公共函数showIndex()
{
$client=new Tumblr\API\client(CONSUMERKEY,ConsumerCret);
$tumblrData=(数组)($client->getBlogPosts(BLOGNAME));
$postViews=array();
foreach($tumblrData['posts']作为$post){
$post=(数组)$post;
$type=TumblrParse::getPostType($post);
$postViews[]=View::make('tumblr..$type,array('post'=>$post));
}
foreach($p的postview){
echo$p;
//这很管用!它可以在发布前正确显示每个帖子视图
//在呈现欢迎视图之前,但我需要它们
//在欢迎视图中的特定位置。
}
返回视图::make('home.welcome')->带有('postViews',$postViews);
}
//views/home/welcome.blade.php
@扩展('layouts.master')
@章节(“标题”)
@家长:欢迎
@停止
@节(“内容”)
你好,世界!
@foreach($p的postview)
{{$p}}//只输出数组的第一个三次
@endforeach
@停止
//视图/布局/post.blade.php
@收益率(‘后内容’)
//views/tumblr/photo.blade.php
//其他帖子类型有自己的视图:video.blade.php、text.blade.php等。
@扩展('layouts.post')
@节(“后内容”)
这是一张照片!
@停止

非常感谢您的帮助!我对Laravel不熟悉,这一点很明显。据我所知,我通常在PHP中做了一些错误,而不是在Laravel中。在本例中,在使用view render方法将每个视图附加到$postViews数组之前,将其呈现为字符串可能是有意义的

$postViews[] = View::make('tumblr.'.$type, array('post' => $post))->render();

关于嵌套视图呢。查看文档。

就是这样!现在您指出了这一点,这是非常有意义的。因为我已经在其他上下文中正确地呈现了这些视图,所以我没有想到要查找该视图方法。非常感谢!我查看了这些,但没有看到任何将不同数据绑定到可能不同的嵌套视图的示例阵列中每个项目的视图。我相信这是可能的,但我没有任何运气。