search
Homephp教程php手册一个简单的模板类(PHP)
一个简单的模板类(PHP)Jun 13, 2016 am 10:50 AM
phpandlandoperatedatatemplateofSimplekindwere ableproject

有了一个数据操作类,这样项目只能简单地操作数据,但是要达到能够和美工一起显示美好的页面,则需要一个比较好的模板引擎。与SMARTY这样比较庞大的模板引擎相比,我想下面这个则真算得小得多了。

这个模板类是我以前在网上看到的,写得比较好,所以我就引用了,还不知道作者是谁,这里就先讲一下这个类的原理。

首先,这个类只有一个简单的正则解析器。但是基本上可以用到了。如果再能够在此基础上扩展,我相相信这个小东西很有发展,请有相同爱好的同志们,来加入到强化他的目地。我这就抛下砖。

template.class.php

 

[html]
class template{ 
     
    //变量储存的数组 
    private $vars = array(); 
    //模板目录 
    public $template_dir = './template/'; 
    //缓存目录  
    public $cache_dir = './cache/'; 
    //编译目录 
    public $template_c_dir = './template_c/'; 
    //模板文件 
    public $template_file = ''; 
    //左连接符 
    public $left_delimiter = '     //右连接符 
    public $right_delimiter = '}>'; 
    //编译文件 
    private $template_c_file = ''; 
    //缓存文件 
    private $cache_file = ''; 
    //缓存时间 
    public $cache_time = 0; 
     
    //内置解析器 
    private $preg_temp = array( 
        '~~i' 
            => '', //  
         
        '~~i' 
            => '', //  
         
        '~~i' 
            => '', //  
         
        '~~i' 
            => '_include($2); ?>', // <?php include('inc/top.php'); ?> 
         
        '~~' => '', //  
         
        '~~' => '', //  
         
        '~ '     ); 
     
    /** 
     *构造器 
     */ 
    public function __construct(){ 
        if(defined('TMP_PATH')){ 
            $this->template_c_dir = TMP_PATH . 'template_c/'; 
            $this->cache_dir   = TMP_PATH . 'cache/'; 
        } 
    } 
     
    /** 
     *变量赋值 
     *@param $key mixed 键名 
     *@param $value mixed 值 
     */ 
    public function assign($key , $value = ''){ 
        if(is_array($key)){ 
            $this->vars=array_merge($key,$this->vars); 
        } 
        else{ 
            $this->vars[$key]=$value; 
        } 
    } 
     
    /** 
     *显示页面 
     *@param $file string 模板文件名 
     */ 
    public function display($file){ 
        echo $this->fetch($file); 
    } 
     
    /** 
     *返回缓存区内容 
     *@param $file string 模板文件名 
     *@return $content string 缓存内容 
     */ 
    public function fetch($file){ 
        $this->template_file = $file; 
        $desc_template_file = $this->template_dir .$file; 
        $desc_content = $this->readfile($desc_template_file); 
         
        $template_c_file_time= filemtime($desc_template_file); 
        //若在超过缓存时间,则编译 
        if($this->cache_time             $this->complie($this->token($desc_content)); 
        } 
        //以下获取缓存区的内容 
        ob_start(); 
         
        @extract($this->vars , EXTR_OVERWRITE); 
        include ($this->template_c_dir . $this->template_c_file); 
         
        $content = ob_get_contents(); 
        ob_end_clean(); 
         
        //$this->store_buff($content); 
        return $content; 
    } 
     
    /* 
     *替换分隔符,以及替换解析器的内容 
     *@param $content string 读取的内容 
     *@return $token_content string 替换后的内容 
     */ 
    public function token($content){ 
        $token_content = $content; 
        if($left_delimiter != '             $token_content = str_replace($left_delimiter , '             $token_content = str_replace($right_delimiter, '}>' , $token_content); 
        } 
        $token_content = preg_replace(array_keys($this->preg_temp), $this->preg_temp , $token_content); 
        return $token_content; 
    } 
     
    /* 
     *生成储存 
     *@param $content string 读取的内容 
     * 
      
    public function store_buff($content){ 
        $this->cache_file = md5($this->template_file) . $this->template_file . '.html'; 
        $tempfile = $this->cache_dir . $this->cache_file; 
        $fp = fopen($tempfile , 'w'); 
        fputs($fp,$content); 
        fclose($fp); 
        unset($fp); 
    } 
    */ 
     
    /* 
     *编译储存 
     *@param $content string 读取的内容 
     * 
     */ 
    public function complie($content){ 
        $this->template_c_file = md5($this->template_file) . $this->template_file . '.php'; 
        $tempfile = $this->template_c_dir . $this->template_c_file; 
        $fp = fopen($tempfile , 'w'); 
        fputs($fp,$content); 
        fclose($fp); 
        unset($fp); 
    } 
     
    /* 
     *读取文件的内容 
     *@param $file string 文件名 
     *@return $content string 文件内容 
     */ 
    public function readfile($file){ 
        if(file_exists($file)){ 
            $fp = fopen($file , 'r'); 
            $content =''; 
            while(!feof($fp)){ 
                $content .= fgets($fp,4096); 
            } 
            fclose($fp); 
            unset($fp); 
            return $content; 
        } 
        else{ 
            exit($file . ' not exist!'); 
        } 
    } 
     
    /* 
     *模板嵌套 
     *@param $file string 文件名 
     *@return string 文件的绝对地址 
     */ 
    public function _include($file){ 
        if(file_exists($this->template_dir . $file)){ 
            return ($this->template_dir . $file); 
        } 
        else{ 
            echo "模板文件不存在"; 
            exit; 
        } 
    }    www.2cto.com

?> 

有了这个模板,你可以忘记那些大型的模板,毕竟你的美工不会在他的切片文件里写那么多SMARTY的标记,而只要他切好,你可以直接用,不用去修改images , css ,js 地址了。


作者:tomyjohn
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
最简便的硬盘序列号查询方式最简便的硬盘序列号查询方式Feb 26, 2024 pm 02:24 PM

硬盘序列号是硬盘的一个重要标识,通常用于唯一标识硬盘以及进行硬件识别。在某些情况下,我们可能需要查询硬盘序列号,比如在安装操作系统、查找正确设备驱动程序或进行硬盘维修等情况下。本文将介绍一些简单的方法,帮助大家查询硬盘序列号。方法一:使用Windows命令提示符打开命令提示符。在Windows系统中,按下Win+R键,输入"cmd"并按下回车键即可打开命

如何在技嘉主板上设置键盘启动功能 (技嘉主板启用键盘开机方式)如何在技嘉主板上设置键盘启动功能 (技嘉主板启用键盘开机方式)Dec 31, 2023 pm 05:15 PM

技嘉的主板怎么设置键盘开机首先,要支持键盘开机,一定是PS2键盘!!设置步骤如下:第一步:开机按Del或者F2进入bios,到bios的Advanced(高级)模式普通主板默认进入主板的EZ(简易)模式,需要按F7切换到高级模式,ROG系列主板默认进入bios的高级模式(我们用简体中文来示范)第二步:选择到——【高级】——【高级电源管理(APM)】第三步:找到选项【由PS2键盘唤醒】第四步:这个选项默认是Disabled(关闭)的,下拉之后可以看到三种不同的设置选择,分别是按【空格键】开机、按组

CS玩家的首选:推荐的电脑配置CS玩家的首选:推荐的电脑配置Jan 02, 2024 pm 04:26 PM

1.处理器在选择电脑配置时,处理器是至关重要的组件之一。对于玩CS这样的游戏来说,处理器的性能直接影响游戏的流畅度和反应速度。推荐选择IntelCorei5或i7系列的处理器,因为它们具有强大的多核处理能力和高频率,可以轻松应对CS的高要求。2.显卡显卡是游戏性能的重要因素之一。对于射击游戏如CS而言,显卡的性能直接影响游戏画面的清晰度和流畅度。建议选择NVIDIAGeForceGTX系列或AMDRadeonRX系列的显卡,它们具备出色的图形处理能力和高帧率输出,能够提供更好的游戏体验3.内存电

广联达软件电脑配置推荐;广联达软件对电脑的配置要求广联达软件电脑配置推荐;广联达软件对电脑的配置要求Jan 01, 2024 pm 12:52 PM

广联达软件是一家专注于建筑信息化领域的软件公司,其产品被广泛应用于建筑设计、施工、运营等各个环节。由于广联达软件功能复杂、数据量大,对电脑的配置要求较高。本文将从多个方面详细阐述广联达软件的电脑配置推荐,以帮助读者选择适合的电脑配置处理器广联达软件在进行建筑设计、模拟等操作时,需要进行大量的数据计算和处理,因此对处理器的要求较高。推荐选择多核心、高主频的处理器,如英特尔i7系列或AMDRyzen系列。这些处理器具有较强的计算能力和多线程处理能力,能够更好地满足广联达软件的需求。内存内存是影响计算

如何通过PHP编写一个简单的在线预约系统如何通过PHP编写一个简单的在线预约系统Sep 26, 2023 pm 09:55 PM

如何通过PHP编写一个简单的在线预约系统随着互联网的普及和用户对便利性的追求,在线预约系统越来越受到欢迎。无论是餐厅、医院、美容院还是其他服务行业,都可以通过一个简单的在线预约系统来提高效率并为用户提供更好的服务体验。本文将介绍如何使用PHP编写一个简单的在线预约系统,并提供具体的代码示例。创建数据库和表格首先,我们需要创建一个数据库来存储预约信息。在MyS

如何使用Java编写一个简单的学生成绩报表生成器?如何使用Java编写一个简单的学生成绩报表生成器?Nov 03, 2023 pm 02:57 PM

如何使用Java编写一个简单的学生成绩报表生成器?学生成绩报表生成器是一个可以帮助老师或教育者快速生成学生成绩报告的工具。本文将介绍如何使用Java编写一个简单的学生成绩报表生成器。首先,我们需要定义学生对象和学生成绩对象。学生对象包含学生的姓名、学号等基本信息,而学生成绩对象则包含学生的科目成绩和平均成绩等信息。以下是一个简单的学生对象的定义:public

快速入门:使用Go语言函数实现简单的图书管理系统快速入门:使用Go语言函数实现简单的图书管理系统Jul 30, 2023 am 09:18 AM

快速入门:使用Go语言函数实现简单的图书管理系统引言:随着计算机科学领域的不断发展,软件应用的需求也越来越多样化。图书管理系统作为一种常见的管理工具,也成为很多图书馆、学校和企业必备的系统之一。在本文中,我们将使用Go语言函数来实现一个简单的图书管理系统。通过这个例子,读者可以学习到Go语言中函数的基本用法以及如何构建一个实用的程序。一、设计思路:我们首先来

如何使用PHP开发简单的文件管理功能如何使用PHP开发简单的文件管理功能Sep 20, 2023 pm 01:09 PM

如何使用PHP开发简单的文件管理功能简介:文件管理功能在很多Web应用中都是必不可少的一部分。它允许用户上传、下载、删除和展示文件,为用户提供了便捷的文件管理方式。本文将介绍如何使用PHP开发一个简单的文件管理功能,并提供具体的代码示例。一、创建项目首先,我们需要创建一个基本的PHP项目。在项目目录下创建以下文件:index.php:主页面,用于显示上传表

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor