2012-10-20 19 views
10

stworzyłem nowy Drupal 7 Theme i próbuje wdrożyć hook_theme na template.php tak:Jak zaimplementować hook_theme w drupal 7?

function mytheme_theme($existing, $type, $theme, $path){ 
    return array(
     'mytheme_header'=>array(
      'template'=>'header', 
      'path'=>$path.'/templates', 
      'type'=>'theme', 
     ), 
    ); 
} 

potem umieszczone header.tpl.php w katalogu szablonów i wyczyszczone wszystkie bufory i wywołać funkcję motywu:

theme('mytheme_header', $vars); 

i header.tpl.php lubi to:

<?php 
fb('calling header template');//the function of FirePHP to output debug info 
print '<div>Header</div>'; 
//... 

sprawdzić Firebug i uzyskać informacje dzwoniąc szablon 'header', to znaczy, nazwał plik header.tpl.php, ale nie wydrukował kodu html. Co jest nie tak z moim kodem?

Odpowiedz

16

Spróbuj dodać tablicę variables w hook_theme

function mytheme_theme($existing, $type, $theme, $path){ 
    return array(
     'mytheme_header' => array(
      'template' => 'header', 
      'path' => $path . '/templates', 
      'type' => 'theme', 
      'variables' => array(
       'title' => NULL, 
       'some_text' => NULL, 
      ), 
     ), 
    ); 
} 

W pliku header.tpl.php:

<h1><?php print $title; ?></h1> 
<p><?php print $some_text; ?></p> 

Następnie wydrukować go tak:

$vars = array(); 
$vars['title'] = "This is a title"; 
$vars['some_text'] = "Some text..."; 
print theme('mytheme_header', $vars); 
+1

'arguments' została zmieniona do 'zmiennych' w Drupal 7 – Clive

+0

Dzięki. Naprawiłem to. :) –

+0

To nie był problem zmiennych. Debuguję z FirePHP i odkryłem, że nazwał on plik header.tpl.php, ale nie wydrukował żadnego kodu html. –

Powiązane problemy