Laravel4 - Trying to create widget like partials that will pass/accept a parameter to get the correct data

StackOverflow https://stackoverflow.com/questions/20979042

Question

Within my views I am trying to reuse a partials, so I can create a list of latest posts anywhere in the site / sidebar by using the blade @include where the passed variable is the number of posts to be included, like this:

@include('widgets.lastestposts', array('numPosts' => '10')

However the problem I have is how to get the Post data for the correct number of posts within the partial?

I could pass through a list of all posts via Post::all() using the controller or even a View::composer and then within the partial use a @for|@endfor loop to only show the correct number based on the 'numPosts' value.

However this doesn't feel right and I am sure there must be a better way than pulling a complete list of Posts when I may only need 5 or 10.

I tried View::composers but I could find how to pass through a variable so I can get the correct number of Posts returned. I can't access the parameter 'numPosts' via

$view->getdata() 

as I expect 'numPosts' needs to be passed to the view via the controller, rather than the Blade file - either that or I messed up!

Am I missing something easy here or is what I am looking to do actually a very bad idea and I should be doing something else?

Any pointers are most gratefully received. Thanks!

(ps - I was looking to be able to do this via the blade file rather than setting up the number of posts in the controller to allow our designers/HTML coders to simply add the widgets and parameters to the views rather than have to mess with controllers.)

Was it helpful?

Solution

I would do this using View Composers. You can pass data to the composer with your include:

@include('widgets.lastestposts', array('numPosts' => '10')

and then from within the view composer you should be able to access that param like so:

View::composer('widgets.latestposts', function($view)
{
   $view_data= $view->getData();
   $post_count = $view_data['numPosts'];

   //You will have to implement something to do this
   $post_data = Post::getLatestPosts($post_count); 

and then you can pass the post data back with:

   $view->with('posts', $post_data);
}

and then from within your blade partial widgets.latestposts you can iterate over $posts to display the posts.

I know you said in your post that you tried this method, but I am fairly certain that this approach should work. Double check all your filenames, file extensions (.blade.php) etc...

Hope this works.

OTHER TIPS

Using a view composer is pretty easy:

You can, for instance, store your number of posts in a Session var:

View::composer('*', function($view)
{
    $view->with('numPosts', Session::get('numPosts'));
}

Or just hardcode them:

View::composer('*', function($view)
{
    $view->with('numPosts', 10);
}

And use it in your view:

<?php $i=1; ?>

@foreach($posts as $post) 
  {{ $post->title }}

  <?php 
      $i++; 
      if ($i > $numPosts) break;
  ?>          
@endforeach

Assuming that you've passed $posts to your view:

$posts = Post::all();

return View::make('your-view')->with('posts', $posts);

But, remember, you also can do things like

$posts = Post::orderBy('created_at', 'desc')->take(Session::get('numPosts'))->get();

// or

$posts = Post::orderBy('created_at', 'desc')->take(10)->get();

// and

return View::make('your-view')->with('posts', $posts);

So you won't have to filter them again in your view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top