Drupal – Introduction to Drupal Theming @ 2

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: http://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!', 'http://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

About these ads

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

  1. Pingback: Drupal – Introduction to Drupal Theming @ 1 | Eureka!

  2. Pingback: Drupal – Introduction to Drupal Theming @ 3 | Eureka!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s