When developing a drupal site, we are sometimes faces the problem of displaying block dynamically, like "Block A" is needed to be displayed only on "Page A".

Sure you can debate that this feature is built-in in the block administration area, but do you ever face a site with 50 block that need to be displayed dynamically per page?

How much time is wasted to configure all those 50 block?

Although my solution is not 100% perfect or fool proof, it surely helps me cut down the time wasted in configuring each block.

The solution is to create a block region for each parents url that we are going to configure.

Let's say we have these following "parents url" :
1. Forum -> "/forum"
2. Blog -> "/blog"
3. user -> "/user"

and we need to configure the block in the "sidebar region"

Now we need to create these block regions :
1. sidebar -> generic sidebar region that will be shown in every other page url beside the three above.
2. sidebar_forum -> only display in the forum area
3. sidebar_blog -> only display in the blog area
4. sidebar_user -> only display in user area
5. sidebar_all -> region that will be displayed in all page (useful for block like advertising block)

To create those regions you need to edit the Your_theme.info and add the following lines :

regions[sidebar_forum] = sidebar forum
regions[sidebar_blog] = sidebar blog
regions[sidebar_user] = sidebar user
regions[sidebar_all] = sidebar all
regions[sidebar] = sidebar

and print the sidebar's region in the page.tpl.php :

<?php
 
print $sidebar_forum;
?>
<?php
 
print $sidebar_blog;
?>
<?php
 
print $sidebar_user;
?>
<?php
 
print $sidebar_all;
?>
<?php
 
print $sidebar;
?>

If you want to use the hard way to select which regions to display at which url then you need to use php conditioning to filter it out, but there is an easier way to do this.

You need to download and install visibility_api, region_visibility and (optional) region_manager module

The region_visibility is the module that will select / filter which region to display at which url.

The region_manager is for further tuning visibility by filtering out a single block to display at which page url

Now using the region_visibility configuration page you can set the visibility logic for each regions, for example :
1. sidebar_forum -> set it to only show in forum and forum/*
2. sidebar_blog -> set it to only show in blog and blog/*
3. sidebar_user -> set it using php code like :


if (arg(0) == 'user' and is_numeric(arg(1))) {
  return true;
} 
else { 
  return false;
}

4. sidebar -> set it to display on all page except blog, blog/*, forum, forum/*, user/*

By now you have a 4 per page regions plus 1 generic regions in your arsenal. you can put which block to show on which page dynamically.

If you need to further set the visibility of single block, for per page setting, it is easier to use the region_manager module gui to set it but if you want to set multiple page like node/* then you it is easier to use the "normal" block visibility setting in the block administration page

As this is not foolproof method but at least it will save some developing type if you have tons of block to configure.