By: korpton
Date: 2007-09-19
Time: 10:51
|
how to write TBS plug-ins as smarty plug-ins?
<?php
function smarty_prefilter_preCompile($source, &$smarty)
{
$file_type = strtolower(strrchr($smarty->_current_file, '.'));
$tmp_dir = 'themes/' . SET_STYLE . '/'; // 模板所在路径
// 处理模板文件
if ($file_type == '.dwt')
{
/* 将模板中所有library替换为链接 */
$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';
$replacement = "'{include file='.strtolower('\\1'). '}'";
$source = preg_replace($pattern, $replacement, $source);
// 检查有无动态库文件,如果有为其赋值
/*
$dynaLibs = getDynamicLibs($globals['_cfg']['template'], $smarty->_current_file);
if ($dynaLibs)
{
foreach ($dynaLibs AS $region => $libs)
{
$pattern = '/<!--\\s*TemplateBeginEditable\\sname="'. $region .'"\\s*-->(.*?)<!--\\s*TemplateEndEditable\\s*-->/s';
if (preg_match($pattern, $source, $reg_match))
{
$reg_content = $reg_match[1];
// 生成匹配字串
$keys = array_keys($libs);
$lib_pattern = '';
foreach($keys AS $lib)
{
$lib_pattern .= '|' . str_replace('/', '\/', substr($lib, 1));
}
$lib_pattern = '/{include\sfile=(' . substr($lib_pattern, 1) . ')}/';
// 修改$reg_content中的内容
$globals['libs'] = $libs;
$reg_content = preg_replace_callback($lib_pattern, 'dyna_libs_replace', $reg_content);
// 用修改过的内容替换原来当前区域中内容
$source = preg_replace($pattern, $reg_content, $source);
}
}
}
*/
/* 在头部加入版本信息 */
$source = preg_replace('/<head>/i', "<head>\r\n<meta name=\"Generator\" content=\"cboard\" />", $source);
/* 修正css路径 */
$source = preg_replace('/(<link\shref=["|\'])(?:\.\/|\.\.\/)?(css\/)?([a-z0-9A-Z_]+\.css["|\']\srel=["|\']stylesheet["|\']\stype=["|\']text\/css["|\'])/i','\1' . $tmp_dir . '\2\3', $source);
/* 修正js目录下js的路径 */
$source = preg_replace('/(<script\s(?:type|language)=["|\']text\/javascript["|\']\ssrc=["|\'])(?:\.\/|\.\.\/)?(js\/[a-z0-9A-Z_]+\.(?:js|vbs)["|\']><\/script>)/', '\1' . $tmp_dir . '\2', $source);
/* 修正模板中对images目录下的链接 */
$source = preg_replace('/((?:background|src)\s*=\s*["|\'])(?:\.\/|\.\.\/)?(images\/.*?["|\'])/is', '\1' . $tmp_dir . '\2', $source);
$source = preg_replace('/((?:background|background-image):\s*?url\()(?:\.\/|\.\.\/)?(images\/)/is', '\1' . $tmp_dir . '\2', $source);
/* 替换相对链接 */
$source = preg_replace('/(href=["|\'])\.\.\/(.*?)(["|\'])/i', '\1\2\3', $source);
}
/**
* 处理库文件
*/
elseif ($file_type == '.lbi')
{
/* 去除meta */
$pattern = '/<meta\shttp-equiv=["|\']Content-Type["|\']\scontent=["|\']text\/html;\scharset=(?:.*?)["|\']>\r?\n?/i';
$source = preg_replace($pattern, '', $source);
/* 替换路径 */
/* 在images前加上 $tmp_dir */
$pattern = '/((?:background|src)\s*=\s*["|\'])(?:\.\/|\.\.\/)?(images\/.*?["|\'])/is';
$source = preg_replace($pattern, '\1' . $tmp_dir . '\2', $source);
$pattern = '/((?:background|background-image):\s*?url\()(?:\.\/|\.\.\/)?(images\/)/is';
$source = preg_replace($pattern, '\1' . $tmp_dir . '\2', $source);
/* 替换相对链接 */
$pattern = '/(href=["|\'])\.\.\/(.*?)(["|\'])/i';
$source = preg_replace($pattern, '\1\2\3', $source);
}
/* 替换文件编码头部 */
if (strpos($source, "\xEF\xBB\xBF") !== FALSE)
{
$source = str_replace("\xEF\xBB\xBF", '', $source);
}
/* 替换smarty注释 */
$pattern = '/<!--[^>|\n]*?({.+?})[^<|{|\n]*?-->/';
$source = preg_replace($pattern, '\1', $source);
/* 替换不换行的html注释 */
$pattern = '/<!--[^<|>|{|\n]*?-->/';
$source = preg_replace($pattern, '', $source);
return $source;
}
?>
|
|
By: Skrol29
Date: 2007-09-20
Time: 19:13
|
Re: how to write TBS plug-ins as smarty plug-ins?
Hi,
Can you detail what is it for?
|
By: korpton
Date: 2007-09-20
Time: 20:45
|
Re: how to write TBS plug-ins as smarty plug-ins?
for example:
we have a website aa.com,it directoy structs is:
/index.php
/global.inc.php
/lib/tbs.class.php
/theme/skin1/index.dwt
/theme/skin1/images/example.jpg
/theme/skin1/images/skin1.css
/theme/skin1/js/example.js
/theme/skin1/lbi/side.lbi
|
now, in theme directory, maybe is more than skin1,skin2,...skinN... etc
in global.inc.php we get setting global php variable as look as:
define('SET_STYLE', '/skin1/');
|
in index.dwt , use example.jpg --> <img src="images/example.jpg"/>
but index.php run it with index.dwt here is bug,
index.php
$tbs=& new tbs();
$tbs->loadTemplate(SET_STYLE.'index.dwt");
$tbs->show();
|
================================
example.jpg not to show ??
in smarty I can use that plus-ins to modify index.dwt <img...>
--------------------------------------------------------------------
$source = preg_replace('/((?:background|src)\s*=\s*["|\'])(?:\.\/|\.\.\/)?(images\/.*?["|\'])/is', '\1' . $tmp_dir . '\2', $source);
|
---------------------------------------------------------------------
and some work modify , css path, js path, index.dwt file replace <!--BeginLibaryItem "/lbi/side.lbi"--> some html tag<!--EndLibraryItem-->
to side.lbi file, etc....
I try write TBS plug-ins, but unSuccessfull!
|
By: Skrol29
Date: 2007-09-20
Time: 22:22
|
Re: how to write TBS plug-ins as smarty plug-ins?
Hi Korpton,
Sorry this forum doesn't support chinese chars yet.
You can easily modify the contents of the templates using a TBS plug-in.
Here is a skeleton of such a plug-in. All you need it feed the code to modify the perterty Source of the TBS instance.
To retrieve the variable $tmp_dir in this plug-in, you can for example use a new TBS property. For example it can be $TBS->TempDir, and from the plug_in you can have it using $this->TBS->TempDir.
<?php
define('TBS_ChangePath','clsTbsChangePath');
$GLOBALS['_TBS_AutoInstallPlugIns'][] = TBS_ChangePath;
class clsTbsChangePath {
// Property $this->TBS is automatically set by TinyButStrong when the Plug-In is installed.
// You can use it inside the following methods.
function OnInstall() {
$this->Version = '1.00'; // Version can be displayed using [var..tbs_info] since TBS 3.2.0
return array('AfterLoadTemplate');
}
function AfterLoadTemplate(&$File,&$HtmlCharSet) {
// You can edit $this->TBS->Source here.
}
}
?>
|
|
By: korpton
Date: 2007-09-20
Time: 22:30
|
Re: how to write TBS plug-ins as smarty plug-ins?
thank skrol29, reTry.....
|
|
Posting in progress.
Please wait...
|