This translation is older than the original page and might be outdated.
Translations of this page?:

命名空間的樣板

如果你想在特定的命名空間底下所建立的新頁面都能出現預設的內容格式, 例如在 address1)這個命名空間底下所建立的新頁面都能自動將通訊錄的表格格式帶出來, 讓新增的頁面只需填寫內容就好. 要想達成這樣的功能, 只需建立 “namespace templates” 檔案就可達成. 因為當建立一個新頁面的時候, DokuWiki 會先找看看這個目錄內是否存在 _template.txt (“namespace templates” 檔案), 如果存在, 則先將這檔案的內容當成新建立頁面的內容.

但是目前 Dokuwiki 並不提供編輯 _template.txt 檔案的功能 – 通常只有系統管理者才能維護這檔案(透過 FTP 或 WebDAV 來修改這檔案)


你可以透過以下的密技, 讓這個檔案透過 wiki 的編輯介面來維護:

  1. 開起 inc/pageutils.php
  2. 找到 function cleanID() 後在 function 開始先增加一行
      global $INFO;
  3. 然後在第 129 行(有出現 trim() ) 改成
      $id = ($INFO['perm'] == 255 ? trim($id,':.-') : trim($id,':._-'));

經過這樣修改後, 你就可以透過 doku.php?id=_template&do=edit 這樣的網址來編輯 namespace template 檔案內容了. 2)


如果你想要允許所有的人都可以編輯 namespace template, 那就將原本定義的 “_template.txt” 修改成 “template.txt”, 方法如下:

  • inc/common.php 內的第 652 行. 將原本定義的 “_template.txt” 修改成 “template.txt”

這樣只要使用者在這個 namespace 有編輯修改的權限, 就可以建立 “template” 的頁面來當成該 namespace 所有新建立頁面的預設內容

所以建立這 template 檔案的方式就可以如下:

  1. 先使用 wiki 在這個 namespace 下建立一個基本內容格式的頁面, Exp. address:template
  2. 透過 FTP 或 WebDAV 將這個檔案複製成 _template.txt Exp. data/page/address/template.txtdata/page/address/_template.txt
template 內可以使用的變數說明
@ID@ 完整的頁面 ID
@NS@ 頁面的 namespace
@PAGE@ 頁面名稱 (不含 namespace 的 ID 而且空白會被替換成底線)
@USER@ 建立這頁面的使用者帳號
@NAME@ 建立這頁面的使用者名稱
@MAIL@ 建立這頁面的使用者 E-Mail
@DATE@ 建立這頁面的日期時間

← 返回 Manual

新的程式碼

..\inc\common.php ORIGINAL

function pageTemplate($data){
  $id = $data[0];
  global $conf;
  global $INFO;
  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
  $tpl = str_replace('@ID@',$id,$tpl);
  $tpl = str_replace('@NS@',getNS($id),$tpl);
  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
  return $tpl;
}

..\inc\common.php NEW

function pageTemplate($data){
  $id = $data[0];
  global $conf;
  global $INFO;
 
  $path = dirname(wikiFN($id));
  $path = str_replace('\\' , '/' , $path );
  $file = $path.'/_template.txt';
 
  if (@file_exists($file)) {
    $tpl = io_readFile($file);
 
  }else{
    $pathmin = $conf['datadir'];
    $pathmin = str_replace('\\' , '/' , $pathmin );
    while ( strlen($path) >= strlen($pathmin) ){
      $file = $path.'/_template_subdir.txt'; 
      if (@file_exists($file)) {
        $tpl = io_readFile($file);
        break;
      }
      $path = substr($path , 0 , strrpos($path , '/'));
    }
 
  }
 
 
  // $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
  $tpl = str_replace('@ID@',$id,$tpl);
  $tpl = str_replace('@NS@',getNS($id),$tpl);
  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
  return $tpl;
}

階層式 _template.txt

如果你希望有階層式的 _template.txt 命名空間的樣板架構 (如果目前的命名空間並沒有定義命名空間的樣板, 希望 Dokuwiki 能夠沿用上一層的命名空間樣板), 那可以依照以下的程式碼來修改現有程式碼

--- common.php.original 2007-10-02 17:32:30.000000000 +0200
+++ common.php  2007-10-02 17:52:50.000000000 +0200
@@ -649,7 +649,12 @@
   $id = $data[0];
   global $conf;
   global $INFO;
-  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
+  $path = dirname(wikiFN($id));
+  do {
+    $tpl = io_readFile($path.'/_template.txt');
+    $path = dirname($path);
+  } while (($tpl == '') && (strlen($path) >= strlen($conf['datadir'])));
+
   $tpl = str_replace('@ID@',$id,$tpl);
   $tpl = str_replace('@NS@',getNS($id),$tpl);
   $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
1) 通訊錄
2) 注意: 這個範例網址只是以根目錄的 namespace template 來作說明, 並不是所有的 namespace 目錄都是這樣的網址
 
zh-tw/namespace_templates.txt · Last modified: 2008/01/21 17:46 by 116.8.150.225
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Imprint Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsGitXRefTranslate