2

So far I have been using this code, which works fine to print third-party module blocks.

<?php
    $block = module_invoke('views', 'block_view', 'og_members-block_1');
    print render($block);
?>

However, now I am trying to print a block that lives within my theme folder and I can't seem to figure out how to do that. How can I achieve this?

13

There is a way to programmatically print a block in a tpl file.

For 'module' defined blocks you can use block_load() see: block_load op api.drupal.org

If you have a block defined through the block interface, it is called a custom block you can use block_custom_block_get($bid) see: block_custom_block_get() on api.drupal.org

$block = block_custom_block_get(1);
$content = $block['body'];
6

You seem to be confusing things a bit.

Your method that you use in your code example is pretty sounds, it invokes hook_block_view for the desire module with a specific block delta. This method works fine because the only way to create blocks is through the hooks that the block module has defined.

This means that a theme can't create a block and you can't load a block from the theme. You would instead need to implement it in a module. Doing so would allow you to use the method you yourself has described.

  • @googletrop, Thanks again for the help, so that means that there is not way to programmatically print a block from within a tpl page ? Not even with a Query? – silkAdmin Jun 24 '11 at 7:34
  • 2
    There is a difference between creating a block (which is done with hook_block in a module) and printing a block in a tpl.php (which can be done like you described yourself). To write a custom module which implements hook_block, see hook_block and block_example.module. – marcvangend Jun 24 '11 at 8:32
2

It's work for me in Drupal 7.

$block = block_load('block', 'block_id');
$output =_block_get_renderable_array(_block_render_blocks(array($block)));
$res_output = drupal_render($res_output);
print render($res_output);
1

Programmatically printing a block in tpl file.

$view = views_get_view("block-name");
print $view->preview("block");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.