zoukankan      html  css  js  c++  java
  • smarty基础原理

    smarty基础原理

    一、html模板页面

    复制代码
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    <div>{$title}</div>
    <div>{$content}</div>
    </body>
    </html>
    复制代码

    二、PHP后台代码

    复制代码
    <?php
    //连接数据库,获得具体数据
    
    //1.引入迷你smart
    require ("minismart.class.php");
    //2实例化Smarty对象
    $smarty=new minismart();
    //将字符串信息设置为模板引擎类的属性信息
    $smarty->assign("title","qqq");
    $smarty->assign("content","aa");
    
    //3调用compile方法,同时传递ceshi.html模板文件参数
    //在该方法中把ceshi.html内部标记替换为php标记
    $smarty->compile("ceshi.html");
    复制代码

    三、模板引擎

    复制代码
    <?php
    //模板引擎类
    class minismart 
    {  
        //给该类声明属性,用于储存外部的变量信息
        public $tpl_var=array();
        //把外部变量设置成内部变量的一部分
        function assign($k,$v)
        {
         $this->tpl_var[$k]=$v;
        }
    
         //"编译模板文件({}标记替换为php标记)"
        function compile ($tpl) //compile编译
        {  //获得模板文件内部具体内容
           $cont= file_get_contents($tpl);//file_get_contents()获取内容
         
        
        //替换 { ---> <?php echo $this->tpl_var["
        $cont=str_replace("{$","<?php echo $this->tpl_var["",$cont);
        
        //替换 } --->"]; ? >
        $cont=str_replace("}",""]; ?>",$cont);
          
        //把生成好的编译内容(php + html 混编内容)放入一个文件里面
        file_put_contents("./shili.html.php",$cont);
          //引入混编文件
        include ("./shili.html.php");
        }
        
    }
    复制代码

    四、混编代码页面

    复制代码
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    <div><?php echo $this->tpl_var["title"]; ?></div>
    <div><?php echo $this->tpl_var["content"]; ?></div>
    </body>
    </html>
    复制代码
  • 相关阅读:
    阿里云安骑士和云盾不能检测连接服务器问题
    UDP反射DDoS攻击原理和防范
    linux查看端口是否开放
    记一次阿里云服务器被用作DDOS攻击肉鸡
    记一次阿里云负载测试
    mysql定时任务event——清理过期数据
    ansible(一)
    基于python实现的三方组件----Celery
    MongoDB
    flask相关使用
  • 原文地址:https://www.cnblogs.com/pangchunlei/p/5701118.html
Copyright © 2011-2022 走看看