Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment (requires an account with 50 reputation)

2 Answers

up vote 2 down vote accepted

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();
share|improve this answer
That did it! It makes perfect sense now that you point it out. Since I had them rendering out properly in other contexts, it didn't occur to me to look for that View method. Thanks so much! – chrisremo May 25 at 2:48
add comment (requires an account with 50 reputation)

What about nesting views. Check documentation.

share|improve this answer
I looked at this but I didn't see any examples of binding distinct data to a potentially different nested view for each item in the array. I'm sure it's possible, but I didn't have any luck. – chrisremo May 26 at 22:51
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.