WildKatana Design

Drupal: Embedding Nodes, Views and Blocks into .tpl files or nodes

This is the first official tutorial I am posting, and I am posting it mainly for myself, since I've had to look this up numerous times. When doing projects for my clients, I often times need to embed or insert the contents of a block or View into a .tpl file. This is actually pretty easy in Drupal 6. Here is what you do:

VIEWS:

To embed a view, simply put this code:

print views_embed_view('View Name');

It's really that easy. Just type the name of the View you want to embed, and there it goes. For those of you who are inquisitive, the actual function call looks like this:

views_embed_view($name, $display_id = 'default');

BLOCKS:

To embed a block, it is a bit more difficult.You will need to know two things:

1) Which module does the block belong to? (If you made the block yourself, then the module is 'block')

2) What is the block_id of the block? You can see this on the blocks page, just hover over your block. (It's not always a number)

  1. $block = module_invoke('block' ,'block', 'view', 3);
  2. print $block['content'];

That is the gist of it. If you want to see what else is in the block that you loaded, just try a print_r($block). There are a few other things in there you might find useful.

NODES:

Embedding Nodes is also pretty easy:

  1. $node= node_load(array('nid' => 2));
  2. print $node->body;

There you go, that pretty much sums it up. You can use these in .tpl files or nodes if you have the php filter on.


Comments:

Testimonials

Great work, communication and understanding, love to work with you again!

More

Why Use Us?

  • Website Design & Development
  • Valid XHTML/CSS Coding
  • Affordable and Honest Pricing
  • E-Commerce Shopping Carts
  • Great Communication
  • Content Management Systems (CMS)
  • Search Engine Optimization (SEO)
  • Satisfaction Guaranteed!