Search code examples
phpmodel-view-controllerlaraveltumblrlaravel-4

Laravel template not looping through array of views


Hoping this is some kind of silly oversight on my part, but I've had little luck finding information about this or other examples of similar usages of Laravel. I'm developing a Laravel 4 site whose content is populated not with a local database, but with posts from a specific Tumblr blog, via the Tumblr API.

Every Tumblr post has a certain type associated with it ("text", "video", "photo", etc.), and each type has entirely different types of content that need to be spit out, so I have a Blade template for each post type, inheriting from a master post Blade template. (Everything is just a stub right now.)

To fill out the front page, in my controller I'm populating an array with those post views ($postViews). What is maddening is that if I loop through $postViews and echo out each individual view in the controller, it contains the proper content--all three views in the array show up on the final site inside their correct templates.

But when I send $postViews off to my welcome view, and then loop through $postViews inside THERE, it renders three instances of ONLY the first view of the array. I have no idea why.

Here's the relevant code. As you can see in the welcome template, I tried looping through $postViews in the welcome view both with native PHP and with the Laravel template syntax. They both exhibit the same behavior: showing only the first of the three posts, three times.

// 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

I really appreciate any help! I'm new to Laravel, which I'm sure is obvious. And for all I know, I'm doing something wrong in PHP generally rather than in Laravel specifically.


Solution

  • In this instance it probably makes sense to render each view as a string before appending it to the $postViews array using the view render method.

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