3

Would appreciate it if someone could let me know how to print a block in node.tpl.php. I basically have a block created through a view and I haven't set a region for that block as I would like to do is display this block in a specific position in all my node pages.

This is what I did in my theme's template file:

function mytheme_preprocess_node(&$vars) {
  $vars['my_block'] = mytheme_block_render('views', 'myblock-block_1');
}

function mytheme_block_render($module, $block_id) {
  $block = block_load($module, $block_id);  
  $block_content = _block_render_blocks(array($block));
  $build = _block_get_renderable_array($block_content);
  $block_rendered = drupal_render($build);
  return $block_rendered;
}

And in my node.tpl.php file, I just do:

<?php print $my_block; ?>

However, this is giving me a memory limit exhausted error. I can get it to work perfectly if I render the block in mytheme_preprocess_page function and print it in page.tpl.php, but I need to print this in node.tpl.php. I tried the following as well:

function mytheme_block_render($module, $block_id) {
  $block = module_invoke('views', 'block_view', 'myblock-block_1');
  return render($block);
}

That is giving me the same memory exhausted error. Is it too later to render a block in the node preprocess function? Why can I render it in the page preprocess function but not in node? Is there some other preprocess function that we can render it in and then have access to it in node.tpl.php?

1

2 Answers 2

7

I think module_invoke() is correct and you can simply call that in your tpl file. However,

use

print $block['subject'];

print $block['content'];

instead of

print render($block);

Also

D6 and earlier:

$block = module_invoke('module_name', 'block', 'view', 'block_delta');
print $block['content'];

D7:

$block = module_invoke('module_name', 'block_view', 'block_delta');
print render($block['content']);

Refer

for more some more help.

6
  • I added that directly in my node.tpl.php but the block is not showing up.
    – SN_26
    Commented Jan 30, 2013 at 7:08
  • Please mention your Drupal version. Refer - drupal.org/node/26502
    – SGhosh
    Commented Jan 30, 2013 at 7:13
  • Edited my answer, added view more info, hope that helps.
    – SGhosh
    Commented Jan 30, 2013 at 7:16
  • I am using Drupal 7.
    – SN_26
    Commented Jan 30, 2013 at 7:21
  • Looks like you have to do a print render($block['content']); Please try that and get back whether that solved it or not.
    – SGhosh
    Commented Jan 30, 2013 at 7:23
1

I used to encounter memory limit problem whenever I installed the Views module on a fresh installation. I usually added ini_set('memory_limit', '512M') in settings.php. It resolved my problem.

In your case, I'm not sure why are you facing this problem when you render your block to the template.

You could also give a try hook_node_view from the module level.

function MYMODULE_node_view($node, $view_mode = 'full', $langcode = NULL) {
    if($view_mode == 'full'){
        $block = module_invoke('views', 'block_view', 'myblock-block_1');
        $node->content['my_block'] = array(     
            '#markup' => render($block['content']), 
        );
    }   
}
2
  • I hate to think about increasing the memory limit. At this point, I can't afford to increase the memory anymore than 256M, which is what I have now. I will give the module level a go and see what happens. Thank you. Still, wished there was a way to do it in the template file.
    – SN_26
    Commented Jan 30, 2013 at 6:44
  • The following also works if you add it directly to node.tpl.php: <?php $block = block_load('views', 'myblock-block_1'); $block_content = _block_render_blocks(array($block)); $build = _block_get_renderable_array($block_content); print render($build); ?>
    – SN_26
    Commented Jan 30, 2013 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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