• Smarty模板的基础


    对前后端进行分离

    如果要用的话,要从网上把smarty文件下载下来,才能用

    smarty的核心是一个类

    建一个php文件,写一个类文件

    <?php
    class smarty
    {
        public $left = "{";    //左分隔符
        public $right = "}";   //右分隔符
        
        public $arr = array(); //存储变量
        
        //向模板里面注册变量
        function assign($key,$value)
        {
            $this->arr[$key] = $value;
        }
        
        //显示模板
        function display($name)
        {
            //找到模板路径
            $url = "/".$name;
            //模板读取
            $str = file_get_contents($url);
            
            //将str里面的某些内容(变量)做了替换
            //将模板里面的<{$aa}> 替换成 <?php echo $arr[aa]
            
            //将替换好的字符串保存到编译好的文件
             file_put_contents($filename,$str);
             
             //将编译好的文件拿到当前页面显示
             include($filename);
            
        }
    }

    来看一下这个方法是什么个原理,看一下效果

    在这个目录下建个php文件

    <?php
    include("../init.inc.php");
    
    $smarty->assign("ceshi","你好"); //注册变量的方法
    
    $smarty->display("test.html");

    然后再在这个目录下写个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>
    <{$ceshi}>
    </body>
    </html>

    然后运行php文件,看看能出来什么效果

     

    它输出了html文件里面的内容

    这样就能知道php文件里面的display是干嘛了,是把html文件里面的模板放到php这个页面来显示

    然后再把里面的变量解析

    这样就能实现了前后端分离了,访问的页面不一定是要显示的页面

    如果html里面用到了图片或样式表,相对于我们访问的文件来找,并不一定是html

     来看一下一个真正的smarty类

    这是从网上下载的一个smarty文件

    有几个文件是自己后来加的,刚下的时候文件没有那么多

    其中这个文件是比较重要的,是smarty的一个入口文件

    再就是这个文件

    点开它,有smarty的核心类

    里面的内容逻辑和刚才写的那个简单文件差不多,它里面有很多成员变量

    libs是smarty的一个核心目录

    里面的plugins是存放插件的一个文件

    sysplugins是存放系统插件

    cache是用来放缓存文件

    configs用来放配置文件

    templates放模板文件

    templates_c 用来放编译好的文件

    来看一下入口文件的内容,也就是init.inc.php里面的内容

    <?php
    
    define("ROOT",str_replace("\","/",dirname(__FILE__)).'/'); //常量ROOT中指定项目根目录
    
    //echo str_replace("\","/",dirname(__FILE__)).'/'; //获取当前文件所在的位置
    
    require ROOT.'libs/Smarty.class.php'; //加载Smarty类文件
    $smarty = new Smarty(); //实例化Smarty对象
    
    
    define("CSS_PATH","/project/css/");
    define("JS_PATH","/project/js/");
    
    //$smarty -> auto_literal = false; //就可以让定界符号使用空格
    $smarty->setTemplateDir(ROOT.'templates/'); //设置所有模板文件存放位置
    //$smarty->addTemplateDir(ROOT.'templates2/'); //添加一个模板文件夹
    $smarty->setCompileDir(ROOT.'templates_c/'); //设置编译过的模板存放的目录
    $smarty->addPluginsDir(ROOT.'plugins/'); //设置为模板扩充插件存放目录
    $smarty->setCacheDir(ROOT.'cache/'); //设置缓存文件存放目录
    $smarty->setConfigDir(ROOT.'configs/'); //设置模板配置文件存放目录
    
    $smarty->caching = false; //设置Smarty缓存开关功能
    $smarty->cache_lifetime = 60*60*24; //设置缓存模板有效时间一天
    
    $smarty->left_delimiter = '<{'; //设置模板语言中的左结束符
    $smarty->right_delimiter = '}>'; //设置模板语言中的右结束符
    
    
    
    
    
    ?>

    在网址输入入口文件的地址,输出的是入口文件的目录

    在入口文件里其实默认的分隔符是花括号,但为了避免出问题就加上了尖括号

  • 相关阅读:
    面向对象--选课系统作业
    面向对象--本章总结
    面向对象--本章总结---练习题
    16 python 异常处理
    5.15 python 面向对象的软件开发&领域模型
    14 元类
    intellijidea课程 intellijidea神器使用技巧 6-2 数据库关联
    intellijidea课程 intellijidea神器使用技巧 6-1 Spring的关联
    intellijidea课程 intellijidea神器使用技巧 5-2 localhistory
    intellijidea课程 intellijidea神器使用技巧 5-1 svn相关
  • 原文地址:https://www.cnblogs.com/qishuang/p/6505206.html
Copyright © 2020-2023  润新知