drupal 6: how to get data from a custom module in

2019-07-06 15:27发布

问题:

im using a custom module to create the contents of my homepage at example.com/frontpage.

in the module i run a query that gets the data i need, in an array. when i return theme('page', $my_array) i get the "homepage inside the homepage", ie the default drupal logo and sitename is displayed a second time in the main content area.

what's the best way to go about this, create a specific tpl.php file, the contents of which should be ... ?

i realise its a very general question but in 2 hours of trying things out and reading tutorials ive gotten not very far at all ...

thanks

回答1:

If I'm understanding your question correctly, all you have to do is return the content without running it through theme_page. theme_page takes your content and wraps it in the site template, so calling it manually in your case is duplicating the template.

An alternate solution is to have your page's callback function not return anything, instead printing the output of theme_page. If a callback function returns no text, the site's template is not included automatically.

<?php

function mymodule_menu() {
  $items = array();

  $items['option1'] = array(
    'title' => 'Front page option #1',
    'access arguments' => array('access content'),
    'page callback' => 'mymodule_option1',
    'type' => MENU_CALLBACK,
  );

  $items['option2'] = array(
    'title' => 'Front page option #2',
    'access arguments' => array('access content'),
    'page callback' => 'mymodule_option2',
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function mymodule_option1() {
  // build HTML content here
  return $content;
}

function mymodule_option2() {
  // build HTML content here
  print theme('page', $content);
  return null;
}


回答2:

You may be better of with 'views' than a custom module, unles the query for the homepage is really weird, views should be better than a custom module for that sort of thing.

If you have your own theme (or are willing to create one) you can use page-front.tpl.php (as explained here)



回答3:

What's in your template file? Is it possible that those elements are being rendered twice because they're being included twice, i.e. once in your theme's page.tpl.php and again in the $content variable (which looks to be generated by your module)?

Many template files have the following structure:

print $head;
print $logo, $search, $navbar, $messages;
print $content;
print $footer, $closure;

If theme('page', $my_array) is responsible for creating the $content variable, and $content already includes $logo and friends, they are going to be rendered twice.

The first step i would take in this instance is implement hook_preprocess_page() in my theme's template.php file, and throwing in some dsm() calls (if you have the devel module installed - which you should), or print_r()'s , to see exactly what is making it to the page template.
HTH



标签: drupal