0

I have block template. How do I output this block on front page just by calling render($page[region_name]), where region_name is the name of a region to which the block is assigned.

Edit: I want to print the whole region assuming it contains only this block.

7
  • That's a little confusing, do you want to print the whole region or just one block from that region?
    – Clive
    Feb 21, 2012 at 11:56
  • @Clive: I want to print the whole region assuming it contains only this block. Feb 21, 2012 at 12:05
  • Ah, go with the answer from @dimitril below
    – Clive
    Feb 21, 2012 at 12:07
  • do I have to create block through admin menu? If I do this then what I write to block body ? Feb 21, 2012 at 12:13
  • Sorry I'm not sure I understand...you would write whatever you want to appear in the block in the block body
    – Clive
    Feb 21, 2012 at 12:19

2 Answers 2

1

You'd be better off handling this in a custom module and declaring the block in there, then you don't need to worry about trying to inject the output from a template file into a custom block (using PHP in block bodies should be discouraged as it uses the evil eval() function).

The following is a generic example:

// Implements hook_theme()
function MYMODULE_theme() {
  return array(
    'MYMODULE_block_template' => array(
      'template' => 'MYMODULE_block_template',
      'variables' => array()
    )
  );
}

// Implements hook_block_info()
function MYMODULE_block_info() {
  return array(
    'my_block' => array(
      'info' => t('My Block')
    )
  );
}

// Implements hook_block_view()
function MYMODULE_block_view($delta = '') {
  $block = array();

  if ($delta == 'my_block') {
    $block['subject'] = t('Block Title');
    $block['content'] = theme('MYMODULE_block_template');
  }

  return $block;
}

In the above code you declare a theme functino called MYMODULE_block_template which is attached to a template file in the root of your module folder (MYMODULE_block_template.tpl.php).

Once you install the module you'll have a block called 'My Block' in the blocks admin page that you will be able to assign to any region. The content of the block will be exactly what's contained in the MYMODULE_block_template.tpl.php file.

2
  • What you proposed has worked with the exception that this row "'template' => 'MYMODULE_block_template.tpl.php'," should be without '.tpl.php' Feb 21, 2012 at 13:56
  • @OleksandrBondarenko You're right sorry about that
    – Clive
    Feb 21, 2012 at 15:04
2
<?php print render(block_get_blocks_by_region('region_name')); ?>
1
  • What if my blocks are added to the region through a Context? Your code doesn't seem to find any blocks in that case...
    – Dor
    Sep 1, 2014 at 4:00

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.