How to create tpl.php file for a custom module page

Step 1: Implement hook menu

function custom_module_menu() {

$items['mypage'] = array(
'title' => 'My module page',
'page callback' => 'custom_module_page', //callback function for this page
'access arguments' => array('access custom module content'),
'type' => MENU_CALLBACK,
);

return $items;
}

Step 2: Implement hook_theme

function custom_module_theme() {
return array(
'custom_module_page' => array (
'template' => 'custom_module_page',
'arguments' => array('variable' => NULL
),

),
}

Step 3: Page call back function
All the variables you defined in the theme_hook go through all the relevant logical processing here.

function custom_module_page(){

//something logical
$name = 'Frank';

if($name != 'Neo'){
$variable = t('I am not the one because my name is !name', array(!name => $name));
}

return theme('custom_module_page', $variable);
}

Step 4: create your template page (tpl.php)

Your template file must be placed in your module folder and must have the same value as the ‘template’ key in the hook_theme array (see step 2).

So for this example our template file will be called custom_module.tpl.php

Here is an example of how the template file may look


<div>

<?php print $variable2; ?>

</div>

Step 5:  Navigate to page location defined in your hook_menu for this example it will be http://www.example.com/mypage

Dont forget to clear your Drupal cache before doing step 5

Advertisements

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