[改良修正] 強化模版&語言函數,讓您的模板更加自定義! [1P]

簡單解釋就是當使用了此插件之後
在分析模板時會採用以下順序

  • 自定義的模板目(可以設定任何位置 比如 ./plugins/插件目錄/templates/)
  • 預設風格目錄(./templates/你預設的風格)
  • Discuz預設風格目錄(./templates/default)


使用範例

  • 比如如果想要讓插件優先找尋自己插件目錄下的模板
    只要將此段文字放置在插件檔內任何位置(一定要放在 showmessage , template 這兩個函數出現之前的任何地方都可以)
    然後只要將模板放在 ./plugins/插件目錄/templates/ 內就可以優先被採用
    define(PTPLDIR, './plugins/'.$pluginmodule['directory'].((!empty($pluginmodule['directory']) && substr($pluginmodule['directory'], -1) != '/') ? '/' : '').'templates/');
  • 將調用模板的函數改為(呼叫之後 會自動定義PTPLDIR
    include template('attachment_center', 0, '模板目錄位置');


開啟 include/global.func.php
找尋以下兩個函數
function language($file, $templateid = 0, $tpldir = '') {
        ..中間省略..
}[/php][php]function template($file, $templateid = 0, $tpldir = '') {
        ..中間省略..
}[/php]替換成以下兩個[php]function language($file, $templateid = 0, $tpldir = '') {

        if (!$templateid && $tpldir == '' && defined('PTPLDIR')) {
                $tpldir = PTPLDIR;
        } elseif (!$templateid && $tpldir != '' && !defined('PTPLDIR')) {
                define(PTPLDIR, $tpldir);
        } else {
                $tpldir = $tpldir ? $tpldir : TPLDIR;
                $templateid = $templateid ? $templateid : TEMPLATEID;
        }

        $languagepack = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.lang.php';

        if(file_exists($languagepack)) {
                return $languagepack;
        } elseif(TEMPLATEID != 1 && $templateid != 1 && TPLDIR != $tpldir) {
                return language($file, TEMPLATEID, TPLDIR);
        } elseif($templateid != 1 && $tpldir != './templates/default') {
                return language($file, 1, './templates/default');
        } else {
                return FALSE;
        }
}[/php][php]function template($file, $templateid = 0, $tpldir = '') {
        global $tplrefresh, $inajax;
        $file .= $inajax && ($file == 'header'
 $file == 'footer') ? '_ajax' : '';


        if (!$templateid && $tpldir == '' && defined('PTPLDIR')) {
                $tpldir = PTPLDIR;
        } elseif (!$templateid && $tpldir != '' && !defined('PTPLDIR')) {
                define(PTPLDIR, $tpldir);
        } else {
                $tpldir = $tpldir ? $tpldir : TPLDIR;
                $templateid = $templateid ? $templateid : TEMPLATEID;
        }

        $tplfile = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.htm';
        $objfile = DISCUZ_ROOT.'./forumdata/templates/'.$templateid.'_'.$file.'.tpl.php';

        if (TEMPLATEID != 1 && $templateid != 1 && TPLDIR != $tpldir && !file_exists($tplfile)) {
                return template($file, TEMPLATEID, TPLDIR);
        } elseif(TEMPLATEID != 1 && $templateid != 1 && !file_exists($tplfile)) {
                return template($file, 1, './templates/default/');
        }
        if($tplrefresh == 1
 ($tplrefresh > 1 && substr($GLOBALS['timestamp'], -1) > $tplrefresh)) {
                if(@filemtime($tplfile) > @filemtime($objfile)) {
                        require_once DISCUZ_ROOT.'./include/template.func.php';
                        parse_template($file, $templateid, $tpldir);
                }
        }
        return $objfile;
}
--------------
根據以下概念參考而來

原帖由 [i]zhicheng[/i] 於 2007-10-5 03:04 發表
對language函數的修改意見
就是刪除了elseif後面的半句$templateid != 1,這句的意思是模板id不是默認模板,並且模板目錄不是默認模板目錄時,才重新定義模板語言目錄。去掉之後變成只要模板目錄不是默認模板目錄就重新定於模板語言目錄,因為前一句if已經判斷出當前模板目錄下沒有需要的語言模板文件,那麼elseif就可以直接重定向了。而去掉那句有一個好處,就是可以將插件的模板文件從模板目錄拉入插件目錄,這樣更方便打包,同時還能使用系統的模板解析函數。
define(PTP,'./plugins/'.$pluginmodule['directory'].((!empty($pluginmodule['directory']) && substr($pluginmodule['directory'], -1) != '/') ? '/' : '').'template/');
        include template('pluginid',0,PTP);
通過以上代碼,以後所有的模板文件都放到插件目錄下的template文件夾即可,調用時要include template('pluginid',0,PTP);的形式。
當然如果DZ能夠修改這裡的處理邏輯使得插件調用時能夠使用插件目錄下的模板文件,找不到再去當前模板目錄去找,再找不到再去默認目錄去找,就更好一些,方便以後插件的打包。
function language($file, $templateid = 0, $tpldir = '') {
        $tpldir = $tpldir ? $tpldir : TPLDIR;
        $templateid = $templateid ? $templateid : TEMPLATEID;
        $languagepack = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.lang.php';
        if(file_exists($languagepack)) {
                return $languagepack;
        } elseif($templateid != 1 && $tpldir != './templates/default') {
                return language($file, 1, './templates/default');
        } else {
                return FALSE;
        }
}
建議改成:
function language($file, $templateid = 0, $tpldir = '') {
        $tpldir = $tpldir ? $tpldir : TPLDIR;
        $templateid = $templateid ? $templateid : TEMPLATEID;
        $languagepack = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.lang.php';
        if(file_exists($languagepack)) {
                return $languagepack;
        } elseif($tpldir != './templates/default') {
                return language($file, 1, './templates/default');
        } else {
                return FALSE;
        }
}
換句話說,我覺得目前的插件系統,還不能將插件所用到的模板收進插件目錄/template還有相應的語言文件等,

另外模板引擎是否會解析使用了語言包形式的插件模板文件,我看模板引擎似乎只解析templates.lang.php,這樣豈不是做插件模板時必須要把語言和模板混寫,如果能同時讀取插件相對應的用於模板解析的語言包就好了。不讓現在寫插件時模板文件裡就會出現中文。不利於編寫支持多語言的插件,雖然不怎麼用得到,但至少說明模板引擎的擴展性仍有缺陷。
複製這篇網址分享給朋友: http://discuz.bluelovers.net/thread-4601-1.html#pid7281

本篇的內容不含引用除另有聲明外,如符合 CC授權條款 則套用 姓名標示-非商業性-相同方式分享 3.0 通用版

套一句黃小琥說過的話:「你可以點歌,但是我可以選歌」

RE: [改良修正] 強化模版&語言函數,讓您的模板更加自定義! [1P]

真厲害
我學了很久還是差太遠了ˊˋ
複製這篇網址分享給朋友: http://discuz.bluelovers.net/thread-4601-1.html#pid9856

TOP

RE: [改良修正] 強化模版&語言函數,讓您的模板更加自定義! [1P]

= =?
每個人的設計方向不同

我是屬於改善 使用者 看不到的部分的(所以我這種人 上班賺不了錢
因為都無法讓自己&客戶滿意
複製這篇網址分享給朋友: http://discuz.bluelovers.net/thread-4601-1.html#pid9861

本篇的內容不含引用除另有聲明外,如符合 CC授權條款 則套用 姓名標示-非商業性-相同方式分享 3.0 通用版

TOP

RE: [改良修正] 強化模版&語言函數,讓您的模板更加自定義! [1P]

#2說的,沒錯..=w="
複製這篇網址分享給朋友: http://discuz.bluelovers.net/thread-4601-1.html#pid12144

本篇的內容不含引用除另有聲明外,如符合 CC授權條款 則套用 姓名標示-非商業性-相同方式分享 3.0 通用版

TOP

RE: [改良修正] 強化模版&語言函數,讓您的模板更加自定義! [1P]

我想問問..是否把include/global.func.php的第一個function language($file, $templateid = 0, $tpldir = '') {   .....換上有23行的碼,而把第二個function language($file, $templateid = 0, $tpldir = '') {   .....換上有32行的碼?
複製這篇網址分享給朋友: http://discuz.bluelovers.net/thread-4601-1.html#pid12785

本篇的內容不含引用除另有聲明外,如符合 CC授權條款 則套用 姓名標示-非商業性-相同方式分享 3.0 通用版

TOP

RE: [改良修正] 強化模版&語言函數,讓您的模板更加自定義! [1P]

admin,是以上這樣的意思嗎?
[可以叫你姐姐嗎?親切一點.xd]
複製這篇網址分享給朋友: http://discuz.bluelovers.net/thread-4601-1.html#pid12798

本篇的內容不含引用除另有聲明外,如符合 CC授權條款 則套用 姓名標示-非商業性-相同方式分享 3.0 通用版

TOP

RE: [改良修正] 強化模版&語言函數,讓您的模板更加自定義! [1P]

回覆 5#5  win 的帖子

function template 跟 function template (有32行) 交換

function language 跟 function language (有23行) 交換

另外 只會找到一次

function  開頭的東西 都只能出現一次

回覆 6#6  win 的帖子

喜歡怎叫都可以 除了大大這種東西之外

下次 連續回文的時候 改用編輯的方式 把新的話 補上去
複製這篇網址分享給朋友: http://discuz.bluelovers.net/thread-4601-1.html#pid12881

本篇的內容不含引用除另有聲明外,如符合 CC授權條款 則套用 姓名標示-非商業性-相同方式分享 3.0 通用版

TOP

強化模版&語言函數,讓您的模板更加自定義!改良強化型

開啟 global.func.php

function language($file, $templateid = 0, $tpldir = '') {
        ..中間省略..
}[/php][php]function template($file, $templateid = 0, $tpldir = '') {
        ..中間省略..
}[/php][php]function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $tpldir) {
        ..中間省略..
}
[/php]以上刪除

找檔案最底下的
[php]?>[/php]
在上面加上
[php]// bluelvoers

function language($file, $templateid = 0, $tpldir = '') {
	if (!$templateid && $tpldir == '' && defined('PTPLDIR')) {
		$tpldir = PTPLDIR;
	} elseif (!$templateid && $tpldir != '' && !defined('PTPLDIR')) {
		define(PTPLDIR, $tpldir);
	} else {
		$tpldir = $tpldir ? $tpldir : TPLDIR;
		$templateid = $templateid ? $templateid : TEMPLATEID;
	}

	$languagepack = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.lang.php';

	if(file_exists($languagepack)) {
		return $languagepack;
	} elseif(TEMPLATEID != 1 && $templateid != 1 && TPLDIR != $tpldir) {
		return language($file, TEMPLATEID, TPLDIR);
	} elseif($templateid != 1 && $tpldir != './templates/default') {
		return language($file, 1, './templates/default');
	} else {
		return FALSE;
	}
}

function template($file, $templateid = 0, $tpldir = '') {
	global $inajax;
	$file .= $inajax && ($file == 'header' || $file == 'footer') ? '_ajax' : '';

	$templateidex = '';

	if (!$templateid && $tpldir == '' && defined('PTPLDIR')) {
		global $pluginmodule;

		$tpldir = PTPLDIR;
		$templateidex = '_ptpldir'.($pluginmodule['pluginid'] ? '_'.$pluginmodule['pluginid'] : '');
	} elseif (!$templateid && $tpldir != '' && TPLDIR != $tpldir && !defined('PTPLDIR')) {
		define(PTPLDIR, $tpldir);
	} else {
		$tpldir = $tpldir ? $tpldir : TPLDIR;
	}
	$templateid = !empty($templateid) && $templateid ? $templateid : TEMPLATEID;

	$tplfile = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.htm';

	$objfile = DISCUZ_ROOT.'./forumdata/templates/'.$templateid.$templateidex.'_'.$file.'.tpl.php';

	if (TEMPLATEID != 1 && $templateid != 1 && TPLDIR != $tpldir && !file_exists($tplfile)) {
		return template($file, TEMPLATEID, TPLDIR);
	} elseif(TEMPLATEID != 1 && $templateid != 1 && !file_exists($tplfile)) {
		return template($file, TEMPLATEID, './templates/default/');
	}

	@checktplrefresh($tplfile, $tplfile, filemtime($objfile), $templateid, $tpldir, $objfile);

	return $objfile;
}

function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $tpldir, $objfile = '') {
	global $tplrefresh;
	if(empty($timecompare) || $tplrefresh == 1 || ($tplrefresh > 1 && !($GLOBALS['timestamp'] % $tplrefresh))) {
		if(empty($timecompare) || @filemtime($subtpl) > $timecompare) {
			require_once DISCUZ_ROOT.'./include/template.func.php';
			parse_template($maintpl, $templateid, $tpldir, $objfile);
			return TRUE;
		}
	}
	return 	FALSE;
}

// bluelovers[/php]

開啟 template.func.php

找
[php]function parse_template($tplfile, $templateid, $tpldir) {[/php]
改為
[php]function parse_template($tplfile, $templateid, $tpldir, $objfile = '') {[/php]

找
[php]$objfile = DISCUZ_ROOT."./forumdata/templates/{$templateid}_$file.tpl.php";[/php]
改為
[php]$objfile = !empty($objfile) ? $objfile : DISCUZ_ROOT."./forumdata/templates/{$templateid}_$file.tpl.php";[/php]

找
[php]function loadsubtemplate($file, $templateid = 0, $tpldir = '') {
        ..中間省略..
}[/php]

[php]function languagevar($var) {
        ..中間省略..
}[/php]
以上刪除

找檔案最底下的[	DISCUZ_CODE_6	]在上面加上
[php]// bluelovers

function loadsubtemplate($file, $templateid = 0, $tpldir = '') {
	global $subtemplates;

	$templateidex = '';

	if (!$templateid && $tpldir == '' && defined('PTPLDIR')) {
		global $pluginmodule;

		$tpldir = PTPLDIR;
		$templateidex = '_ptpldir'.($pluginmodule['pluginid'] ? '_'.$pluginmodule['pluginid'] : '');
	} elseif (!$templateid && $tpldir != '' && TPLDIR != $tpldir && !defined('PTPLDIR')) {
		define(PTPLDIR, $tpldir);
	} else {
		$tpldir = $tpldir ? $tpldir : TPLDIR;
	}
	$templateid = $templateid ? $templateid : TEMPLATEID;

	$tplfile = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.htm';

	if (TEMPLATEID != 1 && $templateid != 1 && TPLDIR != $tpldir && !file_exists($tplfile)) {
		return loadsubtemplate($file, TEMPLATEID, TPLDIR);
	} elseif(TEMPLATEID != 1 && $templateid != 1 && !file_exists($tplfile)) {
		return loadsubtemplate($file, TEMPLATEID, './templates/default/');
	}

	$subtemplates[] = $tplfile;
	return @implode('', file($tplfile));
}

function languagevar($var) {

	if (defined('PLANG') && isset($GLOBALS[PLANG][$var])) {
		return $GLOBALS[PLANG][$var];
	} elseif(isset($GLOBALS['language'][$var])) {
		return $GLOBALS['language'][$var];
	} else {
		return "!$var!";
	}
}

// bluelovers
複製這篇網址分享給朋友: http://discuz.bluelovers.net/thread-4601-1.html#pid62020

本篇的內容不含引用除另有聲明外,如符合 CC授權條款 則套用 姓名標示-非商業性-相同方式分享 3.0 通用版

我活在我自己織的網上
擁有屬於我蜘蛛的一塊網
請不要打擾我 因為我喜歡獨處
請原諒我吃了份你 因為我要生存

TOP

RE: [改良修正] 強化模版&語言函數,讓您的模板更加自定義! [1P]

支持..很有用
複製這篇網址分享給朋友: http://discuz.bluelovers.net/thread-4601-1.html#pid62022

本篇的內容不含引用除另有聲明外,如符合 CC授權條款 則套用 姓名標示-非商業性-相同方式分享 3.0 通用版

TOP