How to insert a block into a node or template in Drupal 7?

后端 未结 15 1091
闹比i
闹比i 2021-01-30 01:46

In Drupal 6, it was easy to insert a block into a template with the following code:

$block = module_invoke(\'views\', \'block\', \'view\', \'block_name\');
print         


        
15条回答
  •  遇见更好的自我
    2021-01-30 02:29

    The module_invoke() function works. However, I found that rendering a block this way apparently won't use a custom template for that block. This might be OK depending upon your needs.

    As commented before in other answers, this works as well and also makes use of custom templates:

    $raw_block = block_load('your-module', 'delta');
    $rendered_block = drupal_render(_block_get_renderable_array(_block_render_blocks(array($raw_block))));
    print $rendered_block;
    

    So, if you have a custom block--your-module--delta.tpl.php template file, it will be used to format the block.

    Source: http://api.drupal.org/api/drupal/includes!module.inc/function/module_invoke/7

提交回复
热议问题