Our theme name is packttheme
for all the mentioned examples in this section.
Suppose our theme name is packttheme
and we have to add three regions, namely frontpage top
, frontpage center
, and frontpage bottom
on the front page. We can do it by performing the following steps:
Replace all occurrences of
mytheme
with the name of your custom theme.Inside
themes/mytheme/template.php
(create thetemplate.php
fi.<?php /** Define the regions **/\function mytheme_regions() { return array( 'left' => t('left sidebar'), 'right' => t('right sidebar'), 'content' => t('content'), 'header' => t('header'), 'footer' => t('footer'), 'frontpage_top' => t('frontpage top'), 'frontpage_center' => t('frontpage center'), 'frontpage_bottom' => t('frontpage bottom'), ]
Now that the regions are defined, go to
page.tpl.php
withinthemes/mytheme
and place the following code where you would like to display the appropriate regions:Code [<?php if ($is_front || strstr($_GET['q'], 'admin/block')) : ?> <div id="frontpage_top" class="frontpage"> <?php print $frontpage_top ?> </div> <?php endif; ?> ]
Repeat this procedure for all the three frontpage regions. You can combine two regions within one
if()
statement to improve performance.To add blocks to these new regions, simply navigate to Administer | Site Building | Blocks.
To add a region in Drupal 7, perform the following steps:
Go to your theme's
.info
file and add your region in the.info
file, for example,[regions[packt-header] = Packt Header]
Add the preceding region in
page.tpl.php
:[<?php if ($page['packt-header']): ?> <?php print render($page['packt-header']); ?> <?php endif; ?> ]
To add Packt Header in
node.tpl.php
, add the following code:[<?php if ($page['packt-header']): ?> <?php print render(block_get_blocks_by_region('packt-header')); ?> <?php endif; ?> ]
Click on the Clear all caches button located at Administration | Configuration | Development | Performance.
The most commonly used code can be found at
These are some tips and advice that needs to be followed while developing Drupal.
Commonly used coding conventions should be followed, but there are some Drupal-specific conventions that should be kept in mind while creating a theme or module.
While coding, we should prefer PHP in HTML. We should try to avoid HTML in PHP while coding templates. For example, the following coding convention should be avoided:
Code [<?php if (!$page) { print "<h2><a href=\"$node_url\">$title</a></h2>"; } if ($submitted) { print "<span class=\"submitted\">$submitted</span>"; } ?>]
Instead of using the preceding code, the following code should be used:
Code [<?php if (!$page): ?> <h2><a href="<?php print $node_url; ?>"><?php print $title; ?></a></h2> <?php endif; ?> <?php if ($submitted): ?> <span class="submitted"><?php print $submitted; ?></span> <?php endif; ?>]
We need to separate the logic from presentation, if there is a code similar to the following one:
Price: Code[<?php print $price; ?>] Tax: Code[<?php print $price * 0.075; ?>]
Instead of writing the preceding way, we should separate the logic from presentation as follows:
Code[<?php $tax = $price * 0.075; ?>]
Always put a semicolon at the end of all the small PHP printing statements as follows:
Code[<?php print $tax; ?>] – YES Code[<?php print $tax ?>] -- NO
There are other coding conventions for standard Drupal coding. You can get those conventions at:
https://drupal.org/coding-standards
You can find more information about cross-browser testing at:
https://drupal.org/node/981614
You can make your theme semantically correct by visiting the following link at: