background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Embedding a view in a page or node layout

Eric.London's picture

Here is how you can generate the html from a view and embed into a page callback or node.

<?php
function MYMODULE_MYFUNCTION() {
 
// ...code...
  // define view name
 
$viewName = 'MY_VIEW';

 
// get the view object
 
$view = views_get_view($viewName);
   
 
// create an array of arguments
  // NOTE: if you are using arguments, you can pass them into this function
 
$viewArgs = array();

 
// create view html
 
$viewHtml = views_build_view('block', $view, $viewArgs, FALSE, $view->nodes_per_block);
   
  if (
$viewHtml) {
   
$page_contents .= "<h3>" . $view->block_title . "</h3>";
   
$page_contents .= $viewHtml;
  }
 
// ...code...
}
?>

In this example, I am using a block view. You can also use a page or embedded layout and adjust as necessary. I find it helpful to show the contents of the view object to see what's available to you. For instance...

<?php
echo "<pre>" . print_r($view, TRUE) . "</pre>";
?>

Geshi

Hi Eric
I'm wondering how you make syntax highlighting in your code.
I think you are using GeSHI module... but do u use a WYSIWYG editor also? It's becoming a nightmare to make them work at the same time, withouth the WYSIWYG editor changes the HTML entities like > <
Thanks

Eric.London's picture

code filter

I'm using code filter: http://drupal.org/project/codefilter (I like geshi as well).

I actually don't use a wysiwyg editor on my blog, I like to keep things simple ;) On the other hand, I install it for clients on every site.