tags:

views:

1041

answers:

3

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

+2  A: 

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;
}
ceejayoz
option 1 works, didnt realise the var name had to be $content. now i see Array on my homepage. so i need to build the full html inside the mymodule_option1 function? arent you meant to put php in html not html in php?
stef
option 2 working even better; thanks ceejayoz!
stef
If you want to make it real Drupalish, you should make a theme function that makes the actual HTML, while your menu callback does the logic. This is what contrib modules do to make it possible to override their output. In a custom site-module one-time-use only, this is not so important, however. Other than that, a good response +1
googletorp
stef, the variable name doesn't have to be $content, it can be anything. It'll need to be a string, not an array, so you may need to use something like `implode` to flatten your array into a string.
ceejayoz
A: 

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)

Jeremy French
Panels + Views would be even better in many cases.
ceejayoz
tried for ages with a colleague using, not working. i need some if else logic inside the view which isnt possible.
stef
that should have been "using views, not working"
stef
i do have my own theme, will explore that now. thanks for the link
stef
A: 

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

threecheeseopera

related questions