免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1585 | 回复: 0
打印 上一主题 下一主题

基于文件系统的多style网站实现php版本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-20 18:26 |只看该作者 |倒序浏览
大部分站点拥有一个 style的.css文件,但是一些 blog之类的多用户程序,支持不同用户不同样式,这种一般是基于模板来实现的。
根据
css中的url
一文中,.css中出现的 url是相对 .css文件的,而不是全局的。那么我们可以在不同文件夹里面,放置不同"样式"的 .css文件,以及 .css中使用到的图片们(url是相对的,只需要写类似./images/logo.gif,移动到任何目录下,.css都能对应到图片)
下面一个php类,它基于文件系统来实现多 style并且供用户自由选择。
dstyle首先声明了默认配置,look:
... ...
define( 'DSTYLE_DEFAULT_DIR', "/style/" );
define( 'DSTYLE_DEFAULT_INI', "style.ini" );
define( 'DSTYLE_DIR', "dir" );
define( 'DSTYLE_NAME', "name" );
define( 'DSTYLE_CSS', "css" );
... ...
默认情况下,dstyle读取 "/style/"目录,并且以目录中的子文件夹来区分各个"样式",读取相应的子文件夹中的 style.ini为该"样式"的配置信息,其中 name字段是该"样式"的名称,css字段是该"样式"的css文件。
比如当前如此目录
style/
+--- +red/
|   +--- style.ini(name=Apple style=red.css ...)
|   +--- red.css
|   +--- images/
|         + ... ...
|
+--- +blakc/
|   +--- style.ini(name=Night style=night.css ...)
|   +--- night.css
|   +--- images/
|         + ... ...
+--- +green/(name=Tree style=grass.css ...)
|   +--- style.ini
|   +--- grass.css
|   +--- images/
|         + ... ...
|
+... ...
dstyle将读取默认的 style文件夹,获取到3个目录,读取相应目录里面默认的配置文件信息 style.ini,将获得3个样式的配置信息,存放在 dstyle->styles里面。当然,dstyle->dirs保存着目录们的数组,dstyle->names里面保存着样式名称的数组,在程序里面可以当作列表来显示出来,供用户们选择。
一旦用户选种了某个样式,比如第2个,保存在 dstyle->style里面。那么按照style.ini的配置,我们写link样式文件时候,用 dstyle->style['css']或者 dstyle->css,就link相应的 night.css了。
比较忙,不再描述也不写演示代码了,自己看源码吧。
?php
define( 'DSTYLE_DEFAULT_DIR', "/style/" );
define( 'DSTYLE_DEFAULT_INI', "style.ini" );
define( 'DSTYLE_DIR', "dir" );
define( 'DSTYLE_NAME', "name" );
define( 'DSTYLE_CSS', "css" );
class DSTYLE
{
    public $style;
    public $count;
    public $styles;
    public $dirs;
    public $names;
    function __construct( $_dir=null, $_selected=1, $_inifile=null )
    {
        if( empty($_dir) || !is_dir($_dir) )
        {
            $this->dir = dirname(__FILE__).DSTYLE_DEFAULT_DIR;
        }
        else
        {
            $this->dir = $_dir;
        }
        if( empty($_inifile) )
        {
            $this->inifile = DSTYLE_DEFAULT_INI;
        }
        else
        {
            $this->inifile = $_inifile;
        }
        $this->count = 0;
        $this->names = null;
        $this->dirs = null;
        $this->styles = null;
        $this->lists();
        if( $this->count>=$_selected )
        {
            $this->style = $this->styles[$_selected];
            foreach( $this->style as $key => $value )
            {
                $this->$key = $value;
            }
            return true;
        }
        else
        {
            return false;
        }
    }
    private function read( $name )
    {
        if( is_numeric($name) )
        {
            $name = $this->lists[$name];
        }
        if( !is_dir($this->dir.$name) || $name=="." || $name==".." )
        {
            return false;
        }
        $fp = fopen( $this->dir.$name."/".$this->inifile, "r" );
        if( ! $fp )
        {
            return false;
        }
        $retval = null;
        $retval['dir'] = $name;
        while( !feof($fp) )
        {
            $lines = trim( fgets( $fp ) );
            $array = explode( '=', $lines, 2 );
            if( count($array)==2 )
            {
                $retval[$array[0]] = $array[1];
            }
        }
        fclose( $fp );
        
        //$this->name = $name;
        return $retval;
    }
    private function lists()
    {
        if( ! $dirs = opendir( $this->dir ) )
        {
            return 0;
        }
        while( $filename=readdir($dirs) )
        {
            if( filetype($this->dir.$filename)=="dir" ||
                !preg_match( '/\.(|\.)$/', $filename ) )
            {
                if( $style = $this->read( $filename ) )
                {
                    $this->count++;
                    $this->styles[$this->count] = $style;
                    $this->styles[$style['dir']] = $style;
                    $this->styles[$style['name']] = $style;
                    $this->dirs[$this->count] =
                        $style['dir'];
                    $this->names[$this->count] =
                        $style['name'];
                }
            }
        }
        return $this->count;
    }
}
//$STYLE = new DSTYLE();
//print_r( $STYLE );
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/41420/showart_1432904.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP