Tuesday, May 31, 2011

Embedding a View using code

$view = views_get_view('my_view_name','display_delta');
$view = views_get_view('my_view_name','display_delta');
$view->render();
if ($view->result)
  {
    $view_contents = views_embed_view('my_view_name','display_delta');
    $title = $view->display['display_delta']->display_options['title'];
    $output.= "

$title

"; $output.= $view_contents; $output.= "
"; }

Do remember
  1. views_get_view gives you the view object.
  2. view->render();
    is a function to render the view so that you get the result of the view and other values.
  3. Once view->render() is executed, you can get the result from view->result;
  4. views_embed_view embeds the html output of the view.
  5. views_embed_view doesn't give you the title of the view.
  6. To get the title of a view for a particular display use
    $view->display['display_delta']->display_options['title'];