How to print views from a theme: use PHP to insert views directly into a template
This is useful for custom home pages (page-front.tpl.php), and can also be used in custom modules that provide pages.
Not the answer! This is for putting a view directly into a node: http://drupal.org/project/insert_view
There are at least two ways to print a view directly from your theme, custom module page, or PHP input format page:
http://groups.drupal.org/node/3673#comment-10768
http://drupal.org/node/156579
Dan and I feel safer with the above way-- printing it through block. To be specific... "I am Dan Hakimzadeh and I approve this message." In this case Agaric's view is named 'actions'.
<?php
//get the view
$view = views_get_view('actions');
//build the view
//$args is an array of arguments to pass in
//$use_pager - boolean, default FALSE
//$limit - int, default 0
//$page - int, default 0
$block = views_build_view('block', $view, $args, $use_pager, 5, $page);
print $block ;
?>
The way I always have done it:
http://drupal.org/node/156579#comment-248386
<?php
//load the view by name
$view = views_get_view('sample_view');
//output the view
print views_build_view('embed', $view);
?>
See these warnings! If your view isn't working, it may have nothing to do with this code. Maybe, your view isn't enabled at all or for the page or block you are calling.
And also:
Comments
Thank you!!
Works great. Had to replace "<?php" and "?>" with the php open tag and close tag respectivley but otherwise fine. I have been looking for the solution all day.
Thanks,
Matt
You're welcome!
Heh, to explain his comment, Matt typed that he replaced
<?php
and?>
, which are normally turned into "<?php" and "?>" by your browser (and so you see that, rather than the confusing HTML entity code that Matt saw and was trying to say had to be replaced).The reason he saw the HTML entities is that we installed the codefilter module since writing the above post, and I just had to go back to edit the post to use php tags and not a code tag around everything, because codefilter makes sure everything in a code tag outputs as is, and actually adds all that cool highlighting you see if you use real php open and closing.
Anyhow if you can't follow that what I'm trying to say is that neither Matt nor I are crazy, and to use the
<?php
?>
tags (although frequently, because they indicate something is PHP and provide automatic formatting, Drupal demonstrations will use them in places where you do not want to use them--- for instance template.php or a module file, which will already have opened the php tag at the top and won't like having it opened again, or places in Drupal's user interface such as views arguments that expect PHP and don't want you to tell it again.
Update for Drupal 6.x
Use below code to get this done in Drupal 6.x
$view = views_get_view($view_name);
if (!empty($view)) {
$output = $view->execute_display($display_id, $view_args),
}
From : http://drupal.org/node/99721#comment-1078995 :)
shorter
i always use this, it's the shortest i know:
<?php
print views_embed_view('view_name','',$argument);
?>
works just with this single line.
still works like this in
still works like this in Drupal 7
+1 ===> views_embed_view
+1 ===> views_embed_view
Post new comment