theme 使用theme 自己的 *.tpl.php, build a custom hook_theme() on a theme

一個module 需要輸出html 的話, 會使用 hook_theme()
但如果一個theme 都需要輸出特定的html, 或者需要使用 *.tpl.php的話
因為Drupal 6 新增了 theme registry,
以前 Drupal 5 的theme 內的theme() 函數都不會自動加到theme registry
要在 template.php 內:

/*
template.php
*/
function [theme_name]_theme(){
  return array(
    'taxonomy' => array(
      'template' => 'taxonomy',
      'arguments' => array('taxonomy'=>NULL),
    ),
  );
}

便可以在theme_name 資料夾內使用taxonomy.tpl.php:

/*
taxonomy.tpl.php
*/
<ul class="links inline">
<?php foreach ($taxonomy as $term) { ?>
  <?php
    $href
= "taxonomy/term/".$term->vid."/".$term->tid;
   
$text = $term->name;
 
?>

  <li><?php print l($text,$href) ?></li>
<?php } ?>
</ul>

完全分離logic 和 html 輸出
也方便的將 html 的建立, 維護部份分工到另一個人身上

ref: http://drupal.org/node/350634

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <h4> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options