1

I created a page which consist of HTML tags. The code for that is the following one:

function mynewpage_menu() {
  $items['mypage'] = array(
    'title' => 'my page title',
    'page callback' => 'mypage_page',
    'access arguments' => array('administer page'),
  ); 
  return $items;
}

function mypage_page() {    
  $output='html code here';
  return $output
}

Is it the right way to generate a page if i have more than 100 lines? What if I have iframe , img and canvas tags and what if I want to include paths and hyper references?

In what other ways can I generate a page containing 100 HTML tags?

Update:
Am I right in associating mynew-page.tpl.php with the following theme function?

function mymodule_theme() {
  return array(
    'mypage' => array(
      'variables' => array('account' => NULL),
      'template' => 'mynew-page',
    ),
  );
}
2

There isn't a limit on the number of lines returned by a page callback, or on the number of tags it can return.
If the output is getting complex, it's rather better to separate the code into different theme functions; for example, for outputting a link there is l(), for outputting an image there is theme('image'), and for outputting a table there is theme('table'). The first rule is to reuse the existing functions.

In Drupal 7, a page callback can also return a renderable array, such as the following one returned by tracker_page():

  $page['tracker'] = array(
    '#rows' => $rows, 
    '#header' => array(t('Type'), t('Title'), t('Author'), t('Replies'), t('Last updated')), 
    '#theme' => 'table', 
    '#empty' => t('No content available.'), 
    '#attached' => array(
      'css' => array(drupal_get_path('module', 'tracker') . '/tracker.css' => array()),
    ),
  );
  $page['pager'] = array(
    '#theme' => 'pager', 
    '#quantity' => 25, 
    '#weight' => 10,
  );
  $page['#sorted'] = TRUE;

  return $page;

In these cases, the page callback doesn't return any HTML tags. This is also the preferred method, in Drupal 7, as it allows to the other modules to easily change the output of another module.

As alternative, you can use a template file for rendering the output. In this case, you need to:

  • Define a theme function that uses the template file; that is what is done from the Node module for theme('node'), which is defined in node_theme() using the following definition (Drupal 7):

    'node' => array(
      'render element' => 'elements', 
      'template' => 'node',
    ), 
    
  • Optionally create a preprocess and/or a process function for the template, their purpose is to add variables to the ones that are passed to the template file. In the case of the Node module, those functions are template_preprocess_node() and template_preprocess_node().

  • Invoke the theme function to produce the output. In the case of theme('node'), this is done as in the following code:

    // Drupal 6.
    return theme('node', $node, $teaser, $page);
    
    // Drupal 7.
    $build += array(
      '#theme' => 'node', 
      '#node' => $node, 
      '#view_mode' => $view_mode, 
      '#language' => $langcode,
    );
    

Update
This is a common error: The template file name and the theme name must not be different; the only difference can be between using hyphens and using underscores. For the rest, the theme name and the template filename must be the same, which means that "mypage" as theme function and "mynew-page" as template filename don't work; if you are going to use "mynew-page" as template file, then the theme function must be theme_mynew_page(), reported in the theme definition as "mynew_page."

3
  • can i create any .tpl.php or like file in module folder and put all the html code in that and refer that file in mynnewpgae_menu or anywhere in my module page? – Mujtaba Haider Sep 8 '11 at 7:35
  • You can. It's enough you associate the template file to a theme function, and call the theme function. – kiamlaluno Sep 8 '11 at 7:36
  • so what is the way associating the file to theme function? a bit more explanation please.. – Mujtaba Haider Sep 8 '11 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.