How to style a programmatically created page with a template? I'm struggling with understanding theming pages with templates. I'm stuck with creating a new template for a programmatically created page. I have a module that shows a download page for a file. The page is basically a form where user can click Submit and get their file downloaded. I construct the whole form programmatically in a page callback. But what if I want to style it with a template? I fail to see how to do it. Here is my (simplified) code:



// Implements hook_menu()
function mymodule_menu()
{
    $items = array();    
    $items['file/%mymodule_mycustomfile/download'] = array(
    'page callback' =&gt; '_mymodule_filedownload',
    'title' =&gt; 'Page Title',
    'page arguments' =&gt; array(1),
    'access callback' =&gt; TRUE,
    'type' =&gt; MENU_CALLBACK,
    );
    return $items;
}

// Implements autoloader %mymodule_mycustomfile
function mymodule_mycustomfile_load($fid)
{
    return ($fid &gt; 0) ? (object)array('fid' =&gt; $fid) : FALSE;
}

// Page callback function
function _mymodule_filedownload($mycustomfile)
{
    return drupal_get_form('_mymodule_filedownload_form', $mycustomfile);
}

// Creates page form
function _mymodule_filedownload_form($form, &amp;$form_state, $mycustomfile)
{
    $form['label'] = array('#type' =&gt; 'item', '#markup' =&gt; 'File ID');
    $form['fid'] = array('#type' =&gt; 'item', '#markup' =&gt; $mycustomfile-&gt;fid);
    $form['submit'] = array('#type' =&gt; 'submit', '#value' =&gt; 'Download');
    return $form;
}

// Submit button click handler
function _mymodule_filedownload_form_submit($form, &amp;$form_state)
{ 
    die('On Submit');
}


You can see that I construct my form fully in _mymodule_filedownload_form(). But I want to have something like file-download.tpl.php for it and put things like Label there. I fail to see how to do it. My problems are:

1) I stumbled upon theme_hook_suggestions and by default Drupal suggests the following template names: page__file, page__file__%, page__file__1, page__file__download. When I created page--file--download.tpl.php and went to /file/1/download, my screen went blank. I do not know how to output my form in this file and I'm afraid I do not even want to restyle the whole page. I only need to style the page content (the form itself) and leave the page alone. Why can't I have file--download.tpl.php if I have node.tpl.php and comment.tpl.php?

2) As a side note, when you execute template_preprocess_page(&amp;$variables) on node pages, you can see the defined $variables['node'] that contains full node object. How is it done? Can I possibly have something like $variables['mycustomfile'] on my file download pages?
In this case your page is a form, so you need to create a form template this way:

Implement hook_form_alter() in a module or template.php and set the form's #theme property:

function _mymodule_filedownload_form($form, &amp;$form_state, $mycustomfile)
{
    ...
    $form['#theme'] = array('mymodule_filedownload_form');
    ...

}


Then implement new theme:

/**
 * Implements hook_theme().
 */
function hook_theme($existing, $type, $theme, $path){
  $path = drupal_get_path('module', 'mymodule');
  return array(
    'mymodule_filedownload_form' =&gt; array(
      'render element' =&gt; 'form',
      'template' =&gt; 'form--mymodule_filedownload_form',
      'path' =&gt; $path . '/templates',
    ),
  );
}


And then add form--mymodule_filedownload_form.php template with follow code to render complete form (you can render it also by components):

&lt;?php print drupal_render_children($form) ?&gt;