Categories > TinyButStrong general >

how to write TBS plug-ins as smarty plug-ins?

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
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 . '/'; // &#27169;&#26495;&#25152;&#22312;&#36335;&#24452;

    // &#22788;&#29702;&#27169;&#26495;&#25991;&#20214;
    if ($file_type == '.dwt')
    {
        /* &#23558;&#27169;&#26495;&#20013;&#25152;&#26377;library&#26367;&#25442;&#20026;&#38142;&#25509; */
        $pattern     = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';
        $replacement = "'{include file='.strtolower('\\1'). '}'";
        $source      = preg_replace($pattern, $replacement, $source);

        // &#26816;&#26597;&#26377;&#26080;&#21160;&#24577;&#24211;&#25991;&#20214;&#65292;&#22914;&#26524;&#26377;&#20026;&#20854;&#36171;&#20540;
        /*
        $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];
                    // &#29983;&#25104;&#21305;&#37197;&#23383;&#20018;
                    $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) . ')}/';
                    // &#20462;&#25913;$reg_content&#20013;&#30340;&#20869;&#23481;
                    $globals['libs'] = $libs;
                    $reg_content = preg_replace_callback($lib_pattern, 'dyna_libs_replace', $reg_content);

                    // &#29992;&#20462;&#25913;&#36807;&#30340;&#20869;&#23481;&#26367;&#25442;&#21407;&#26469;&#24403;&#21069;&#21306;&#22495;&#20013;&#20869;&#23481;
                    $source = preg_replace($pattern, $reg_content, $source);
                }
            }
        }
        */

        /* &#22312;&#22836;&#37096;&#21152;&#20837;&#29256;&#26412;&#20449;&#24687; */
        $source = preg_replace('/<head>/i', "<head>\r\n<meta name=\"Generator\" content=\"cboard\" />",  $source);

        /* &#20462;&#27491;css&#36335;&#24452; */
        $source = preg_replace('/(<link\shref=["|\'])(?:\.\/|\.\.\/)?(css\/)?([a-z0-9A-Z_]+\.css["|\']\srel=["|\']stylesheet["|\']\stype=["|\']text\/css["|\'])/i','\1' . $tmp_dir . '\2\3', $source);

        /* &#20462;&#27491;js&#30446;&#24405;&#19979;js&#30340;&#36335;&#24452; */
        $source = preg_replace('/(<script\s(?:type|language)=["|\']text\/javascript["|\']\ssrc=["|\'])(?:\.\/|\.\.\/)?(js\/[a-z0-9A-Z_]+\.(?:js|vbs)["|\']><\/script>)/', '\1' . $tmp_dir . '\2', $source);

        /* &#20462;&#27491;&#27169;&#26495;&#20013;&#23545;images&#30446;&#24405;&#19979;&#30340;&#38142;&#25509; */
        $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);

        /* &#26367;&#25442;&#30456;&#23545;&#38142;&#25509; */
        $source = preg_replace('/(href=["|\'])\.\.\/(.*?)(["|\'])/i', '\1\2\3', $source);
    }

    /**
     * &#22788;&#29702;&#24211;&#25991;&#20214;
     */
     elseif ($file_type == '.lbi')
     {
        /* &#21435;&#38500;meta */
        $pattern = '/<meta\shttp-equiv=["|\']Content-Type["|\']\scontent=["|\']text\/html;\scharset=(?:.*?)["|\']>\r?\n?/i';
        $source  = preg_replace($pattern, '', $source);

        /* &#26367;&#25442;&#36335;&#24452; */

        /* &#22312;images&#21069;&#21152;&#19978; $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);

        /* &#26367;&#25442;&#30456;&#23545;&#38142;&#25509; */
        $pattern = '/(href=["|\'])\.\.\/(.*?)(["|\'])/i';
        $source = preg_replace($pattern, '\1\2\3', $source);
     }

    /* &#26367;&#25442;&#25991;&#20214;&#32534;&#30721;&#22836;&#37096; */
    if (strpos($source, "\xEF\xBB\xBF") !== FALSE)
    {
        $source = str_replace("\xEF\xBB\xBF", '', $source);
    }

    /* &#26367;&#25442;smarty&#27880;&#37322; */
    $pattern = '/<!--[^>|\n]*?({.+?})[^<|{|\n]*?-->/';
    $source  = preg_replace($pattern, '\1', $source);

    /* &#26367;&#25442;&#19981;&#25442;&#34892;&#30340;html&#27880;&#37322; */
    $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.....