MVC开发模式以及Smarty模板引擎的使用
发布时间:2020-12-25 10:29 所属栏目:53 来源:网络整理
导读:Linux 全局安装 composer 将目录切换到/usr/local/bin/ 目录 cd /usr/local/bin/ 在 bin 目录中下载 composer curl -sS https: // getcomposer.org/installer | php 通过 composer.phar - v 查看 composer修改为中国镜像 composer.phar config -g repo.packa
Linux 全局安装 composer 将目录切换到/usr/local/bin/目录 cd /usr/local/bin/ 在 bin 目录中下载 composer curl -sS https://getcomposer.org/installer | php 通过 composer.phar -v 查看 composer 修改为中国镜像 composer.phar config -g repo.packagist composer https:packagist.phpcomposer.com 最后我把composer.phar复制了一份,重命名为composer 以后可以直接用composer MVC架构模式 实现简易模板引擎: composer.json { 自动加载 可以在composer.json的autoload字段找那个添加自己的autoloader "autoload": { psr-4: { App\\Controllers\\": Controllers/,App\\Models\\Models/Tools\\Tools/ } } } Models/Users.php <?php model层数据库操作演示 namespace App\Models; class Users { 数据存入数据库演示 public function store() { echo 'store into database'; } 查询数据库演示 function getUsername() { 查询数据库 return test-data; } } Controllers/UserController.php <? App\Controllers; use Tools\Tpl; use App\Models\Users; UserController extends Tpl { function create() { echo User create function getUser() { 通过Model查询数据 $userModel = new Users; $username = $userModel->getUsername(); 将$username显示在对应的一个HTML文件当中,并且显示出来 表现层 user/user.html 将变量发送给模板(html文件) $this->assign(usernameage',20); 显示模板 $this->display(user/user.html); } } Views/user/user.html <!DOCTYPE html> <html lang=en"> <head> <meta charset=UTF-8"> <title>Title</title> </head> <body> <h2> {$username} </h2> <h3> {$age} </h3> </body> </html> Tools/Tpl.php <? 分配变量 将变量发送给模板 Tools; Tpl { protected $assign_vars = []; function assign($tpl_var_name,$val) { $this->assign_vars[$tpl_var_name] = $val; } function display($tpl_name) { Views/user/$tpl_name $className = get_called_class(); App\Controllers\UserController $dirName = strtolower(substr(substr($className,1)">16),1)">0,-10)); user $dir = dirname(__DIR__) . /Views/' . $dirName . / . $tpl_name; file_get_contents $content = file_get_contents($dir); preg_replace foreach ($this->assign_vars as $tpl_var_name => $val) { $content = preg_replace(/\{\$' . $tpl_var_name . \}/<?php echo $this->assign_vars[""]; ?> compile $compile = dirname(__DIR__) . /runtime/Compile/' . md5($tpl_name) . .php; file_put_contents($compile,$content); include include $compile; } } Smarty模板引擎的使用 服务端开发部分演示
<?/** * Created by PhpStorm. */ session_start(); require './libs/Smarty.class.php'; $smarty = Smarty(); 简单配置 初始化设置 $smarty->setTemplateDir('./Views'); $smarty->setCompileDir('./runtime/Compile'$smarty->setConfigDir('./Config'$smarty->addPluginsDir('./Plugins'$smarty->setCacheDir('./runtime/Cache'); $smarty->caching = 1;开启缓存 $smarty->setCacheLifetime(60*60*24$smarty->left_delimiter = '{'; $smarty->right_delimiter = '}'; 缓存机制 if (!$smarty->isCached('extends.html',1)">$_SERVER['REQUEST_URI'])) { 数据库查询 $data = [[]]; 使用 $smarty->assign([ 'username' => 'test-data','age' => 20 ]); 数组 $smarty->assign('arr1',[1,2,3]); $smarty->assign('arr2',['id' => 1,'username' => 'zhangsan','age' => 30$smarty->assign('users', [ ['id' => 1,'age' => 30],1)"> ['id' => 2,'username' => 'lisi','age' => 40] ]); $smarty->assign('hobby_ids',1)">$smarty->assign('hobby_output',['看书','敲代码','看视频'$smarty->assign('options',1)"> [ 1 => '看书',2 => '敲代码',3 => '看视频' 注册function $smarty->registerPlugin('function','font',1)">function ($attributes) { $text = $attributes['text']; $color = $attributes['color'] ?? 'black'; return '<span style="color: ' . $color . '">' . $text . '</span>'; }); 注册变量修饰器 $smarty->registerPlugin('modifier','link',1)">$text,1)">$href,1)">$isCapitalize = false$return = '<a href="' . $href . '">' . $text . '</a>'if ($isCapitalize) { return ucwords($return); } ; }); 注册块状函数 $smarty->registerPlugin('block',1)">$attributes,1)">$text$href = $attributes['href']; if (!is_null()) { return '<a href="' . ; } }); 对象 $smarty->assign('obj',1)">$smarty); $smarty->assign('text','This is a paragraph!'$smarty->display('smarty.html'); $smarty->display('loop.html'$smarty->display('single_tag_func.html'$smarty->display('modifier.html'$smarty->display('block_func.html'$smarty->display('plugins.html'$data); } $smarty->clearCache('extends.html',1)">]); clearAllCache(); $smarty->display('extends.html',1)">$_SERVER['REQUEST_URI']); 前端开发部分演示 (编辑:ASP站长网) |
相关内容
网友评论
推荐文章
热点阅读