User login

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:

http://agaricdesign.com/if-a-direct-call-to-a-view-isnt-working-check-that-you-didnt-break-the-view-quite-indepe...

Resolution

Searched words: 
this could be Drupal development, or even print view directly drupal call view from theme drupal agaric

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 &lt;?php and ?&gt;, 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

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.