Drupal 7版型開發(theming)與Drupal 6的不同(下)


作者: | 2010/05/08 | 3 則迴響


所有模版的標題都具有前置字與後置字變數

Drupal 7樣版都具備兩個新的標準變數:$title_prefix$title_suffix

這兩個變數內放的是想要被呈現在個別的標題前或後的輸出內容,可作為contextual links之用,像是讓具有某種權限的使用者可以看到一些特別的連結出現在標題上面或下面。

在node.tpl.php內局部的(Granular)展示$content 資訊

現在不需要像使用Drupal 6的$content時,要先做處理才能分隔$content的輸出。在Drupal 7使用 print render($content) 等同於在 Drupal 6的 print $content,不過當您想輸出部份的內容,像是link,在Drupal 7可以用 print render(content[‘links’])取得。

使用方式可以參考一下 node.tpl.php內的程式碼:

[code]
<div class=”content”>
<?php
// 先把comments跟links的內容藏起來
hide($content[‘comments’]);
hide($content[‘links’]);
// 輸出 $content 其他的內容
print render($content);
?>
</div>

// 現在我們才輸出 links 跟 comments 的內容
<?php print render($content[‘links’]); ?>
<?php print render($content[‘comments’]); ?>.
[/code]

在輸出整個$content之前,先把我們想要在稍後才輸出的comments跟links先使用hide來隱藏,之後再個別輸出我們想要的順序及樣式。

Field 加入核心

如果您知道CCK這個模組,標題的這句話等同於 CCK加入核心了;如果不熟悉CCK是什麼,簡單的說,Field就是能讓你自訂新的內容型態(透過增加一些特定的欄位,就能成為新的內容型態)。這跟版型開發有啥關係嗎?嗯,因為我們也能在module/field/theme內找到field.tpl.php,來針對field作一些樣式修改。

box.tpl.php消失了

它原本是用來包覆節點內的回應部份與搜尋結果,除了很少用到,又跟區塊(block)容易搞混。

版型內的搜尋項目消失了,移到區塊(block)上

原本要在page.tpl.php內呼叫搜尋表單的情況,現在變成放進區塊內了。

模版建議符號變更

模版建議符號變成 ‘- -‘ (中間沒有空白,因為中間不留空白,wp會直接輸出成dash),而不再是drupal 6的’-‘,例如:
* page- -front.tpl.php
* node- -blog.tpl.php
* node- -event-performance.tpl.php
注意,一個元素的文字區隔依舊使用’-‘。

兩階段的變數處理函式和template_preprocess_html()

第一階段是現存的preprocess(參考http://drupal.org/node/223430),而第二階段是process函式,是接在preprocess之後被執行。另外,多了一個template_preprocess_html 函式,用來預先處理 (preprocess) html.tpl.php內的變數。

Garland 版型內的template.php可以找到template_preprocess_html函式的使用範例:

[code]
/**
* Override or insert variables into the html template.
*/
function garland_preprocess_html(&$vars) {
// Toggle fixed or fluid width.
if (theme_get_setting(‘garland_width’) == ‘fluid’) {
$vars[‘classes_array’][] = ‘fluid-width’;
}
// Add conditional CSS for IE6.
drupal_add_css(path_to_theme() . ‘/fix-ie.css’, array(‘weight’ => CSS_THEME, ‘browsers’ => array(‘IE’ => ‘lt IE 7’, ‘!IE’ => FALSE), ‘preprocess’ => FALSE));
}

[/code]

RDFa

Drupal透過採用RDFa (屬性資源描述框架,Resource Description Framework-in-attributes) 技術,可以透過後設資料來延伸網頁資料的語意,算是語意網 (Semantic Web) 相關技術之一。Drupal 7透過設定html.tpl.php內的幾個標籤屬性,而能夠輸出RDFa,這些相關標籤有DOCTYPE、html、head,屬性設定如下:

[code]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML+RDFa 1.0//EN” “http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”<?php print $language->language; ?>” version=”XHTML+RDFa 1.0″ dir=”<?php print $language->dir; ?>”
<?php print $rdf_namespaces; ?>>

<head profile=”<?php print $grddl_profile; ?>”>
[/code]

預設的html.tpl.php均已標記這些屬性了。若您想多了解RDFa,可參考維基百科有關RDFa的說明

目前Drupal 7版型開發的兩個待處理的問題

1. 條件式的樣式表
這個問題是關於是否應該要讓版型師能夠在模版的.info檔內宣告有條件的樣式表 (http://drupal.org/node/522006)。

目前的替代方案是,要把判斷的程式碼放進preprocess函式內,但這需要對PHP有基本的認識,對一般的前端開發者是不盡理想的。不過當然Drupal 7版型開發過程中,若對PHP有一定的認識,是非常有幫助的。目前要作版型開發的建議背景,的確還無法完全脫離對 PHP 的一點認識,但未來發展當然會希望能完全分離,各司其職。

2. 缺乏品質良好的模型開發說明文件

  • 目前Drupal 7的theming文件是過時的
  • 文件裡頭沒有關於html.tpl.php的描述,因此都錯置成page.tpl.php

參考資訊:
http://pingv.com/blog/a-peek-at-drupal-7-theme-system-changes
http://drupal.org/update/theme/6/7

<div class=”content”>
<?php
// We hide the comments and links now so that we can render them later.
hide($content[‘comments’]);
hide($content[‘links’]);
print render($content);
?>
</div>

<?php print render($content[‘links’]); ?>

<?php print render($content[‘comments’]); ?>.


標籤:, , ,

分類:, , ,

本文作者是Audi Lu

3 則留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。

*

*

*

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料