How To: Embed a View

The views module is one of the most powerful drupal modules. One of the things that makes it so powerful is the ability to not only build pages and blocks with views but to embed views in the display of other modules or embed them in your themes .tpl.php files. When I first stared embeding views I used the method outlined in the drupal manual. But, then I discovered there was an easier way to embed them. Let's take a look at it.

The manual says to use some code that looks something like:

<?php
//load the view by name
$view = views_get_view('faq_topics');
//output the top three items in the view with the node title as an argument
print views_build_view('embed', $view, array($title), false, 3);
?>

This code first loads the views information and them builds the actual view to print. I have three issues with this code:

  • This code is logic. In keeping with the separation of layers this would violate that by bringing logic into the presentation layer.
  • What if views changes the way that views works. If views changes this means there is some work to do with changing your code. And, more places this could happen with two functions being called. This may seem small, and it is, but it's something to consider.
  • Something about this seems that you should be able to do this in one function. If this is the standard way for modules and themes to embed views why have two functions to do it.

Luckily, there is a much easier way that solves my concerns above. Views has a function called theme_view that rolls all of this together. This means we can take advantage of the theming system.

So, if you want to embed a view, like the one above, you'd put in something like:

<?php
print theme('view', 'faq_topics', 3, false, 'embed', array($title));
?>

This would display the same thing as the code above. But, it's theme code and it's one function.

The arguments for this function are:

<?php
theme
('view', $view_name, $limit = NULL, $use_pager = NULL, $type = 'embed', $view_args = array())
?>

The documentation on the arguments:

/**
* Returns a themed view.
* @param $view_name
*    The name of the view.
* @param $limit
*   Maximum number of nodes displayed on one page. if $limit is set and $use_pager is
*   not, this will be the maximum number of records returned. This is ignored
*   if using a view set to return a random result.
*   If NULL, the setting defined for the $view will be used.
* @param $use_pager
*   If set, use a pager. Set this to the pager id you want it to use if you
*   plan on using multiple pagers on a page. Note that the pager element id
*   will be decremented in order to have the IDs start at 0.
*   If NULL, the setting defined for the $view will be used.
* @param $type
*    'page' -- Produce output as a page, sent through theme.
*      The only real difference between this and block is that
*      a page uses drupal_set_title to change the page title.
*    'block' -- Produce output as a block, sent through theme.
*    'embed' -- Use this if you want to embed a view onto another page,
*      and don't want any block or page specific things to happen to it.
* @param $view_args
*   An array containing the arguments for the view
*/

It's as simple as that. Happy drupaling...

Couldn't you just use a views block?

When I needed to embed views into our front page, we just used the "Block" option in views creation. How is this different?

places besides regions

There are a lot of places besides regions or as the main content to put views. For example, without this type of functionality I couldn't have built this page. This is just one example. This allows me to use views in custom modules to generate content where I used to write custom queries.

So, there is a lot of use for something like this. Usually to create something Bob dreamed up.

Insert View module

Hi Matt, Love the podcast!

In addition to the code (above) can you please share your thoughts on the Insert View module;

Thanks

Russ

Theme

Does this method allow you to them this both individually when inserted in to a block?

I'm a little new, so I'm still trying to wrap my head around this.

The View Theming

You can theme a page view, a block, and a view embedded in a page differently if you like. Or, you can theme them the same. It's partially dependent on your situation and partially on the CSS you use.

If you're new I'd suggest learning how CSS is setup in drupal and how the theme system works. Then do some tests with this. At that point it should be fairly easy for you to pick up.

Drupal 6

Any ideas how to get this to work in Drupal 6?
Has something changed from D5 to D6?

Thanks in advance

When Drupal 6 Views is Released

When drupal 6 Views is released I'll look into it. Wouldn't surprise me if it's already in the drupal 6 views documentation.

Don't know what I found?

I did some searching and found this...
http://drupal.org/node/234621

Looking for a little advice...

My programming skills are minimal. I have never "patched" anything... Is this typical for Drupal?

Should I just "wait" it out?

thanks again.

I'd hold off

You might want to wait if you aren't good at programming. The patch you reference won't help you embed a view in Drupal 6. And, to get views to work in drupal 6 requires you to alter drupals core, at this point.

I'd wait it out. Views is not ready for prime time in drupal 6 and if you're not comfortable mucking around in the code you could easily break something.

The wait shouldn't be too much longer. Remember, "Patience is bitter, but it's fruit is sweet."

True That

Thanks Matt.

I believe that I will continue using D6 for one project that is still about 3 months out from pre-production (thanks for the podcast on pre-pro, loved that one!!) Which means I still have 5 months before I need to worry about embeding views.

The other site is ready for me to start mashing together modules (gonna Rhino it... a G&G term I have begun to use) so I will head back to D5 to get it up and running in the next month or so.

Thanks for your input, I wanted to make sure I wasn't "wimping" out of using D6, but I think there are some really legit reasons to stick with D5 for right now...

If you get a chance, and if needed, please update this post when D6 is ready, I use this page to refer back to :)

Keep up all the good works!

Embedding views with Views 2

Hit this one today, found the answer here:

<?php
 
print views_embed_view($view_name, $display_id = 'default');
?>

Its also mentioned in the on-going Views 2 docs. Lovely.

Thanks

Thanks. For anyone interested, here is the basic documentation for this function.

/**
* Embed a view using a PHP snippet.
*
* This function is meant to be called from PHP snippets, should one wish to
* embed a view in a node or something. It's meant to provide the simplest
* solution and doesn't really offer a lot of options, but breaking the function
* apart is pretty easy, and this provides a worthwhile guide to doing so.
*
* @param $name
*   The name of the view to embed.
* @param $display_id
*   The display id to embed. If unsure, use 'default', as it will always be
*   valid. But things like 'page' or 'block' should work here.
* @param ...
*   Any additional parameters will be passed as arguments.
*/

Smooth

Thanks,
I saw this today come up on Drupal Planet... I came here to post a link and... it was already here!!!

Love the Drupal Community!!

Thanks Paul and Matt!

Thank Earl, not me!

Thank Earl, not me!

elegant and effective!

thanks for the post Matt - an elegant and effective solution; can't wait to try this out on one of my sites.

FYI I was have also been looking into a CCK field add-in called "Viewfield", which sorta does a similar thing - it allows you to include the output from a view as a CCK field type which you then theme in the usual manner...

But the above approach looks a whole lot easier - and I can "hide it" in the .tpl.php files so the client can't accidentally break it.

Cheers
Pete

Excellent

Good suggestion. Well done!