Embed views into a Drupal node.tpl

You are here

On a project early this year I found the need to add a view of images between the content type body and comments. I looked at context module as I have used it before. But on reflection the website was very small and I didn't see the need to install an extra module for this small job.  After some research I found the function views_embed_views this allows the developer to add a view to a tpl file using code. This post shows you how you can do it for yourself. Firstly create a view that has a view page and view block and call the view anything you like. I have called mine views_embed_view (not very imagitive I know).  


Next create a content type basic page in my case yes you guessed it, I called it views_embed_view. Inside the content area of the page I added Welcome as you can see. We are going to use this to display the view you create. But this could be any content type this is only for the example.


As a part of the api views_embed_view() needs the machine name of your view. To find out what that is go to the views page via Structure link and then hover over the view name that you created this will give the machine name. Another way is to edit the view and look at the url on your browser bar. In either case make a note of what it says.



Next we need to open node tpl file this is usually in <your_theme_name>/node.tpl.php. If you can't find it you can go to your parent theme and copy and paste node.tpl.php into the theme directory or create a file yourself (note you will have to clear the caches if you do either of these steps). Next add the code below anywhere you would like in the file and save it. As you can see in the picture I have wrapped them in a div you don't have to do but I found it easier to style.

<?php print views_embed_view('views_embed_view',"default"); ?>

How does the code work you may ask? The views_embed_view function needs the view machine name as the first variable in this case my view name is  views_embed_view. If a second variable is not added it will automatically use default. In this case I am displaying the default for the example this calls the page view that is stored in view name  views_embed_view that I created at the start.



If you are to change code like the what is below from default to page in the node.tpl file it give same view as the default.

<?php print views_embed_view('views_embed_view',"page"); ?>


This is what it looks like in the browser when you andd the code to the node.tpl.php as you can see below the welcome file the view is now a part of the content type. 


If we go back to the node.tpl.php and modify the code a little we can see how flexible this function is. When you created the block in your view it was given a name and in this case it was block_1. If you change it to that and then save your file it will call the block and display it instead of the page view.

<?php print views_embed_view('views_embed_view',"block_1"); ?>


This is what it looks like when the block_1 is used instead of page or default.


When you are on a smaller job that does not need a lot of context this is ideal . It  could be used for artilces you want to promote on the bottom of your blog or any number of other things.One thing I did find is how get it to display the view title. I believe it can't be done. Other that that is saved me installing another module to maintain on this build.