in drupal 7, views_get_view('VIEWNAME') get a error? I want to embed a view in the tpl.php, so I am using the views_get_view('VIEWNAME') function.Here is what I am doing the tpl.php.

&lt;?php
   $view = views_get_view('MapView');  
   print $view-&gt;preview('default');
?&gt;


"MapView" is the view's name, I am not sure the argument in the views_get_view is the machine name or human readable name, in fact in my example is using the human readable name, because I can't find the machine name in the drupal view configuration

anyway, I get a error saying "Fatal error: Call to a member function preview() on a non-object", seems like I didn't fetch the view in the correct ?

any idea ?
Even thought the documentation for views_get_view() refers to $name as the name of the view, what the function wants is the machine name of the view.

You should always check the value returned from the function is not NULL, as the function doesn't return any value, if it doesn't find the view with that name.

  ctools_include('export');
  $view = ctools_export_crud_load('views_view', $name);
  if ($view) {
    $view-&gt;update();
    return $view-&gt;clone_view();
  }


views_embed_view(), for example, uses the following code.

  $view = views_get_view($name);
  if (!$view || !$view-&gt;access($display_id)) {
    return;
  }

  return $view-&gt;preview($display_id, $args);


You should use similar code, avoiding to call $view-&gt;access() if the view should always be visible, independently from the permission the currently logged-in user has. If you need to show the view only when the user has the permission to see it, then you should consider using views_embed_view().