六、实战——注册表单验证/七、仿Smarty简易模板引擎

六、实战——注册表单验证

1.前台页面准备,html

<form action="b.php" method="post">
    用户名<input type="text" name="username" id="username" value="" />
    <br><br>
    email<input type="text" name="email" id="email" value="" />
    <br><br>
    <input type="submit" value="注册">
</form>

2.注册表单验证,php

require_once 'a.php';#引入文件

$regex = new regexTool();
if (!$regex->noEmpty($_POST['username']))
    exit('用户名不能为空');
if (!$regex->isEmail($_POST['email']))
    exit('email格式错误');

七、实战——仿Smarty简易模板引擎

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/17 0017
 * Time: 上午 11:36
 */
class template{
#1.模板引擎类-成员属性
    private $templateDir;#存储模板引擎源文件的所在目录
    private $compileDir;#存储编译之后文件的存放目录
    private $leftTag = '{#';#模板需要替换的变量,做标记
    private $rightTag = '#}';
    private $currentTemp = '';#存储当前正在编译的模板文件
    private $outputHtml;#存放当前正在编译中的html代码
    private $varPool = array();#把模板中需要用到的变量放到改模板中使用

#2.模板引擎类-构造函数
    public function__construct($templateDir,$compileDir,$leftTag=null,$rightTag=null){
        $this->templateDir = $templateDir;
        $this->compileDir = $compileDir;
        if (!empty($leftTag)) $this->leftTag = $leftTag;
        if (!empty($rightTag)) $this->rightTag = $rightTag;
    }

#3. 模板引擎类-写入和获取数据
    public function assign($tag,$var){
        $this->varPool[$tag] = $var;#把模板中需要的变量都放到这
    }

    /**
     * 获取数据
     * @param $tag
     * @return array
     */
    public function getVar($tag){
        return $this->varPool[$tag];
    }

#4. 模板引擎类-获取模板源文件
    /**
     * 获取模板的源文件
     */
    public function getSourceTemplate($templateName,$ext = '.html'){
        $this->currentTemp = $templateName;
        $sourceFilename = $this->templateDir.$this->currentTemp.$ext;
        $this->outputHtml = file_get_contents($sourceFilename);
    }

#5. 模板引擎类-模板编译
    /**
     * 对源文件进行编译
     */
    public function compileTemplate($templateName = null,$ext = '.html'){
        $templateName = empty($templateName) ? $this->currentTemp : $templateName;
        //核心代码,正则替换
        $pattern = '/'.preg_quote($this->leftTag);
        $pattern .= ' *\$([a-zA-Z_]\w*) *';#满足php变量的命名规则
        $pattern .= preg_quote($this->rightTag).'/';
        $this->outputHtml = preg_replace($pattern,'<?php echo $this->getVar(\'$1\'); ?>',$this->outputHtml);

        $compileFilename = $this->compileDir.md5($templateName).$ext;
        file_put_contents($compileFilename,$this->outputHtml);

    }

#6.模板引擎类-显示模板
    public function display($templateName = null, $ext = '.html'){
        $templateName = empty($templateName) ? $this->currentTemp : $templateName;
        include_once $this->compileDir.md5($templateName).$ext;
    }
}

7.正则替换测试

$pattern = '/\{#';
$pattern .= ' *\$([a-zA-Z_]\w*) *';#满足php变量的命名规则
$pattern .= '#\}/';
$subject = 'aaa:{#$test#}';
$a = preg_replace($pattern,'<?php echo $$test ?> ',$subject);
echo $a;

8. 模板引擎测试

index.php文件代码

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/17 0017
 * Time: 下午 2:18
 */
require_once 'template.php';
$baseDir = str_replace('\\','/',dirname(__FILE__));
$temp = new template($baseDir.'/source/',$baseDir.'/compiled/');

$temp->assign('title','厉害了1');
$temp->assign('test','我很厉害');

$temp->getSourceTemplate('index');
$temp->compileTemplate();
$temp->display();

index.html文件代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{#$title#}</title>
</head>
<body>
    测试:{#$test#}
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值