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?
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.
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.
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'));
精彩评论