ASU Web Community

Views: Place Drupal views precisely on your page

This article talks about displaying views you've created in Drupal anywhere you'd like on your site. Typically, when you create a Drupal view, you'll provide a page or block view so that the view can be accessed. However, sometimes you don't want to have a page with a table of data-you'd rather have something more styled or content embedded in a precise location of your snazzy front page. Don't worry, there are some back-end view functions to accomplish just that.

Say, for example, you have a glorious front page for your drupal site that you've created. You've managed to come up with a page-front.tpl.php and styled it to your liking. Now, when you add views to that page, they may show up anywhere. You can avoid that by placing them where you want them to be. This positioning can also be accomplished with css, but I found this block of code to be a simpler alternative.

One benefit to this method is that you don't need to edit the view's block (if you provide block view) to only display the block on the front page. You can just input the following code directly into your page-front.tpl.php.

<?php


$view = views_get_view('your_view_name_here');


$view->page_type = 'list';


echo views_build_view('embed', $view);


?>

Looks simple, eh? We're first calling the views_get_view function to retrive your view's contents into an array called $view. Then, we specify that the page_type (table, list, teaser) should be list. We did this to make the site as accessible as possible, as it will use ul's and li's. Then, we simply 'build' the view that you created in the first section ($view) and echo it all in one statement. Viola! You've now got a snippet of code that will allow you to display a Drupal view anywhere on any page! There is a lot more that can be done with the views arguments-here is a good place to start learning about the more advanced features.