TheLQ
TheLQ

Reputation: 15008

Require and place block in a drupal theme?

In a theme I'm creating I'm wanting to place blocks in locations that are different from the default ones. Eg place the search block in the header.

Is there any way to do this from a theme: Be able to require a block and place it somewhere?

Upvotes: 1

Views: 268

Answers (3)

SpaceBeers
SpaceBeers

Reputation: 13947

In D7 you can add a placeholder region in your themes .info file, clear the cache and put your block in there you can then output the region's content.

print render(block_get_blocks_by_region('region_name'));

Upvotes: 0

Oswald
Oswald

Reputation: 31647

The theming system of Drupal is flexible enough to turn exactly that part of the page into a region where you want your block to appear. For this you need to add the region to the theme's .info file as described on Assigning contents to regions, e.g regions[foobar] = Foobar. This will make a new variable $page['foobar'] available to your page.tpl.php. You can then assign content to the region the same way as with Drupal's builtin regions.

If you want to circumvent this system and display a block ad-hoc, have a look at how _block_render_blocks renders blocks.

Upvotes: 1

rik
rik

Reputation: 8612

It defeats the idea of configurable blocks but nevertheless it's possible: You have to create empty block templates for all but the allowed region. Or you provide/override the theme function for this very block and return nothing for prohibited regions.

Upvotes: 0

Related Questions