You posting only a small bit of the problem isn't going to help.
What is $this->tweets
? A collection/array of objects? How are they retrieved? What's in the partial view you're using to render a single tweet?
Apologies -- I've been banging my head against it so long I guess I can't explain it very well or think properly which bits are most relevant. Let's be more specific.
The controller runs
echo "debug 1: "; print_r($socialData->tweets);
return View::make('public.social', ['socialData' => $socialData]);
$socialData
is an object which has many methods on it which are called from the public.social
view -- things to get the title of the social feed, its URL, and getFeedHtml
to print out the feed itself. One such object (the Twitter one) has a property $tweets
which is an array of data structures describing tweets, pulled from the Twitter API or cache.
So in public.social
we have, for instance,
<div class="social-feed">
debug 2: {{{ print_r($socialData->tweets, true) }}}
{{ $socialData->getFeedHtml() }}
</div>
That method looks a little like the following:
$html = '';
foreach ($this->tweets as $tweet) {
echo "debug 3: "; print_r($tweet);
$html .= View::make('public.social.twitter.tweet', [
'date' => Carbon::parse($tweet->created_at, Config::get('app.timezoneDisplay')),
'tweet' => $tweet,
])->render();
}
return $html;
The public.social.twitter.tweet
view includes a chunk like this:
<div class="social-message">
debug 4: {{{ print_r($tweet, true) }}}
{{ SocialTwitterData::getMessageHtml($tweet) }}
</div>
The getMessageHtml
static method is looking at the object and turning it into HTML (in particular, it's wrapping links around hashtags etc).
In the above setup, 100% of the time I get the first tweet five times, rather than five different tweets. You'll see I put debug messages in the above.
Debug 1 (controller, before main view starts to render) shows the correct data: the array of five tweets.
Debug 2 (in the main view) shows the same.
Debug 3 (in the getMessageHtml
method, for each tweet) shows five separate tweets (correct).
Debug 4 (in the tweet partial) shows the first tweet's data structure five times.
Next, as I said in my original post, if I run getMessageHtml
in the controller and pass the HTML it generates to the main view and then have the view just echo that, I get correct output for 90% of the feeds I've run it on. On the other 10% I still get the first tweet repeated five times. I have no idea why this might be.
Does this clear up the issue? Is there any more information I can give which might help?
I'm still stuck on this. Anyone able to help?
A suggestion I have is to run a for
loop instead of a foreach
, or even manually repeating them out if need be, just to see if the foreach
itself is holding onto the data. Or even clone the $tweet
variable before sending it to the view.
Thanks, beaverusiv, but I'm pretty sure the debug output above shows that the data passed to the view was just fine -- only its output seemed to be holding on to old values.
But yesterday after my last post here I spent some time boiling the situation down to the basics, seen at https://github.com/laravel/framework/issues/4090 and I became convinced it's a bug in the templating system.
It turns out it's an issue with the sections. Using @overwrite to end the sections rather than @endsection (@endsection is the same as @stop) clears it up for me.
I still believe it's a bug, because at least in my test case there's nothing already assigned to the section which could be overwritten anyway as far as I can see, but this works as a solution for now.
Thanks for your solution, tremby. I have the same problem using renderSections() on a foreach loop for HTML table rows, and @overwrite seems to make it work.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community