Yesterday we have talked about the simplest way to theme Drupal content
Drupal – Introduction to Drupal Theming @ 1
But this is not a very good approach because printing inline HTML/PHP inside the theme function is not a good programming practice. This time, i want to create a .tpl.php for the block so we could separate the backend and frontend coding.
Create the following module.
ykyuen.info
; $Id$ name = Theme customized block description = Theme the customized block. Ref: https://ykyuen.wordpress.com package = "YKYUEN" core = 6.x version = 6.x-1.0
ykyuen.module
<?php
/**
* Implementation of hook_block().
*/
function ykyuen_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Eureka');
$blocks[0]['cache'] = BLOCK_NO_CACHE;
return $blocks;
case 'view':
switch ($delta) {
case 0:
$block['content'] = theme('eureka', 'Welcome to Eureka!', 'https://ykyuen.wordpress.com');
break;
}
return $block;
}
}
/**
* Implementation of hook_theme().
*/
function ykyuen_theme() {
return array(
'eureka' => array(
'arguments' => array(
'name' => NULL,
'link' => NULL
),
'template' => 'eureka',
)
);
}
In the above .module file, we didn’t define the theme function. Instead, we add the template file name inside the hook_theme() function. So let’s create the eureka.tpl.php in put it in the module root folder.
eureka.tpl.php
<h2>Theme .tpl.php example</h2> <p><a href="<?php print $link; ?>"><?php print $name; ?></p>
Enable the block and check it out.

Done =)
Next: Drupal – Introduction to Drupal Theming @ 3
Reference: Drupal – Introduction to Drupal Theming @ 1

2 thoughts on “Drupal – Introduction to Drupal Theming @ 2”