tpl

改變 form 的 template

這次修改的是 views 的 exposed form
需要比較大的修改便想要使用 tpl 而不使用 template.php
舊文 form 也可以使用template (*.tpl.php)也有說明, 但已經不是 Drupal6.x 作法了

以下更新為 Drupal6.x 的作法:
先自建一個模組, 使用 hook_theme
定義如下:

<?php
function mymodule_theme($existing, $type, $theme, $path) {
  return array(
   
//form id
   
'form_id' => array(
     
'arguments' => array('node' => NULL),
     
'template' => 'search_form', //表示使用 search_form.tpl.php
   
),
  );
}
?>

重點在於提供一個 template 的值, 你便可以在該 tpl 檔使用 <?php $form; ?>
再使用 <?php drupal_render($form['title']); ?> 之類完成 form 的 各欄位和 html 碼

最後, 實作的時候記得清緩存就可以了

Drupal contact form use tpl.php 將聯絡表單的內容移到 tpl.php

其實我做的是一個contact module 的擴充, cck 的 email field
它可以建立一個送到特定 email 欄的聯絡表單, 是建基於內建的聯絡表單的擴展
而我需要將它的 html 完全改變, contact form 的修改應該很類似

步驟:
先在theme 注冊一個使用的 theme hook:

<?php
//template.php
mytheme_theme() {
 
$hooks['email_mail_page_form'] = array(
   
'template' => 'email-mail-page-form',
   
'arguments' => array('form' => NULL),
   
// other theme registration code...
 
);
  return
$hooks;
}
?>

然後在 theme 內新建一個 email-mail-page-form.tpl.php:

//email-mail-page-form.tpl.php
<?php
kprint_r
($form);
?>

但和前文所記, 如果需要使用 hook_form_alter() 的話
便需要自建一個 module, 而不可以好像上例般直接在 theme 內修改

進階 views 模版(二): HTML 列表 Complex views templating, part 2, table, HTML list templates

part1

補一下 table 顯示之下的 theming.

假如你的 table 有4個 fields, views 的 theming information 便有6個 templates:
1個 display 的 tpl (page 或block)
1個 style tpl, 包含了 <table>
每1個 field 都可以有一個自己的 tpl (詳見 attachment)

HTML list (ul, ol) 也是同樣的情況
display + style tpl, 外加每個field 一個tpl
也可以做 field by field 的 tpl, 修改 <ul> 的class, <li>多個 span 之類

最後, 雖然和 views 無關, 但pager 也經常由 views 產生
但 views 的 pager 都是使用 core 的pager, theme_pager()
theme 的時候沒有 tpl, 要在 template.php 修改, 或者使用preprocess 提供 tpl