Profile picture for user admin
Daniel Sipos
21 May 2013

In this article I am going to show you how to embed a View in a template file (.tpl). Using a cool Views API function, you can render the display of any View and even pass it arguments.

In an earlier post I showed you a cool module that allows content editors to insert Views in their content using a very handy syntax. I also wanted to show you a more developer oriented approach to getting a similar result.

If you are theming or something and work a lot with Views, you should know about views_embed_view(). Using this function, you can embed Views and even dynamically pass them filter arguments.

Similar to the use case I talked about in the last post (but not quite), say you have a View block that shows a few related Articles to the one currently being read. You want these titles appearing right after the Article body but before the comments. You can go into the node.tpl.php file, simply embed it there and you are done.

I have to make a mention here though. It is never a good idea to do this kind of thing directly in the .tpl file. You should always try to keep your logic separate from the presentation. In this case, you should perform this action in the template's preprocess function and pass the rendered View along as a variable to be printed out in the .tpl.

That being said, a quick way to check if it works is the following:

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

$name is the name of the View and $display_id is the machine name of the Display you want. If you only have one or you are fine with the default one, no need to specify the Display. Additionally though, you can add further parameters that get passed to the View as arguments. So for the use case above, you’d do something like this:

<?php print views_embed_view('related_posts', 'block', $node->nid); ?>

This embeds the Display ‘block’ of a View called ‘related_posts’ and passes the node ID (nid) of the node currently being rendered as a filter argument. You have to then set up the View to get filtered based on this argument.

PS: If you are unsure of the machine name of the display, just edit the View and under the Advanced tab you’ll find the Machine name option where you can manually set it too.

Hope this helps.

Profile picture for user admin

Daniel Sipos

CEO @ Web Omelette

Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.

Contact us

Comments

Oskar Calvo 21 May 2013 13:40

About it.

If you use "views_embed_view" in a tpl template you are doing a "performance error". You will re-load the Drupal bootstrap. This is a performance problem.

Also, if you use "views_embed_view" you are developing coupling between the theme and the content. You should use context as a "controller".

Also, if you want to update it you should rewrite code, with context you will not need it only disable the context.

The best option is:
Build a new region in the theme.
Use context to configure when to print the view into the regions.
Use feature to export the configuration.

Oskar

Daniel Sipos 21 May 2013 16:09

Hey Oskar,

Hey Oskar,

Thanks for the warning and the good tip!

Anonymous 21 May 2013 20:13

As a general rule, never ever

As a general rule, never ever print on a tpl anything that is not available to it already.
Use any of the hook_preprocess and hook_process hooks add content to the tpl.

Thomas 21 May 2013 22:21

Question

Hi Danny,

Thanks for this post. I wonder what would be an argument to use this instead of, for example, creating a panel?

Regards,

Thomas

Daniel Sipos 21 May 2013 22:00

Thanks for all the good

Thanks for all the good comments. My main objective in this article was to show the existence of this cool function that can come in handy in various cases.

I would not advise either to use it directly in the .tpl file (although I agree it might seem like I did :) ), but rather in a preprocess function to maintain logic separate from presentation.

@Thomas, depends on what your needs are. I wanted to show a developer way of calling a View - there's a function for that :)

Thanks for the good comments!

Oskar Calvo 22 May 2013 10:21

In reply to by admin

If you use it in the

If you use it in the preprocess function is the same as if you use it in the tpl files.

There is two ways, use it in a hook_page_alter (Use this hook when you want to remove or alter elements at the page level, or add elements at the page level that depend on an other module's elements (this hook runs after hook_page_build().) or use with context/block regions or another modules, but you should try to never attach to a preprocess or tpl.

This is my advice.

Also, if you want to explain a function the best way is to make a real example of the function at the same time to avoid problems.

Oskar

Elliot 21 May 2013 23:00

Display Suite & others..

Display Suite can be also used to add a block field that renders a view with a block display. You can use contextual filters as normal, plus have the ability to use DS's layout's to further customize your content.

I find having a combination of the 'omega' theme along with the 'omega_tools' module, and also 'delta', 'ds', 'context' & 'custom_formatters' modules, you generally shouldn't have to go near your tpl.php files. Though often getting by without 'context' and 'delta', to keep things a little simpler.

Plus most things are exportable, making life easier in the future too. :)

Diseño web Tarragona 06 Aug 2014 03:56

Hi Daniel,

Hi Daniel,

Thanks for this post. Very nice info!

Hossain 15 Aug 2014 19:23

A Video Tutorial Could Be Great!

Hi Denial I think Video Tutorials could be great! Even though I still with written tutorial concept specially when I am using mobile phones to read your site. But when I am using Laptop I am willing to see a video tutorial than reading a long blog. Just a suggestion nothing serious. However I am new in php and learning through your and some other authors posts. So I must give you thanks for all those hard works which help us to learn everything from scratch but 100% free. :)

Cesar 25 Nov 2014 19:14

Is this approach correct?

Hi, your article was very interesting. I was not sure if I was making the things the correct way. I sumarize what I do, but I find it so cumbersome.

  1. Create a new kind of content, i.e. "News".
  2. Create another kind of content, "News List".
  3. I create many "News" in different Languages and Domains (i18n and Domain Access).
  4. I create different Views with different Contextual filters to get "News" items.
  5. I create a content of type "News list".
  6. In template.tpl.php, I suggest the tpl: page--newslist.tpl.php
  7. In this tpl, I hide the main content.
  8. In template.tpl.php I embed all the Views as you say in your post, processing different parameters from URL, context, etc.
  9. I print these Views results in the page--newslist.tpl.php

It works, but... is it a correct approach? Isn't it too extremely complicated?

Add new comment