linux 全局安装 compor
将目录切换到/usr/local/bin/目录 cd /usr/local/bin/在 bin 目录中下载 compor curl -ss /d/file/titlepic/installer | php通过 compor.phar -v 查看 compor修改为中国镜像 compor.phar config -g repo.packagist compor https://packagist.phpcompor.com最后我把compor.phar复制了一份,重命名为compor以后可以直接用compor
mvc架构模式
控制器(controller)- 负责转发请求,对请求进行处理
视图(view) – 界面设计人员进行图形界面设计
模型(model) – 程序员编写程序应有的功能(实现算法等等)、数据库专家进行数据管理和数据库设计(可以实现具体的功能)
实现简易模板引擎:
compor.json
{ // 自动加载 // 可以在compor.json的autoload字段找那个添加自己的autoloader "autoload": { "psr-4": { "app\\controllers\\": "controllers/", "app\\models\\": "models/", "tools\\": "tools/" } } }
models/urs.php
<?php // model层数据库操作演示 namespace app\models; class urs { // 数据存入数据库演示 public function store() { echo 'store into databa'; } // 查询数据库演示 public function geturname() { // 查询数据库 return 'test-data'; } }
controllers/urcontroller.php
<?php namespace app\controllers; u tools\tpl; u app\models\urs; class urcontroller extends tpl { public function create() { echo 'ur create'; } public function getur() { // 通过model查询数据 $urmodel = new urs; $urname = $urmodel->geturname(); // 将$urname显示在对应的一个html文件当中,并且显示出来 // 表现层 ur/ur.html // 将变量发送给模板(html文件) $this->assign('urname', $urname); $this->assign('age', 20); // 显示模板 $this->display('ur/ur.html'); } }
views/ur/ur.html
发誓英文 <!doctype html> <html lang="en"> <head> <meta chart="utf-8"> <title>title</title> </head> <body> <h2> {$urname} </h2> <h3> {$age} </h3> </body> </html>
tools/tpl.php
<?php // 分配变量 // 将变量发送给模板 namespace tools; class tpl { protected $assign_vars = []; public function assign($tpl_var_name, $val) { $this->assign_vars[$tpl_var_name] = $val; } public function display($tpl_name) { // views/ur/$tpl_name $classname = get_called_class(); // app\controllers\urcontroller $dirname = strtolower(substr(substr($classname, 16), 0, -10)); // ur $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["' . $tpl_var_name . '"]; ?>', $content); } // compile $compile = dirname(__dir__) . '/runtime/compile/' . md5($tpl_name) . '.php'; file_put_contents($compile, $content); // include include $compile; } }
smarty模板引擎的使用
服务端开发部分演示
smarty引擎的安装
变量的分配和加载显示模板
以插件形式扩展smarty
缓存控制技术
smarty.php
<?php /** * created by phpstorm. */ ssion_start(); require './libs/smarty.class.php'; $smarty = new smarty(); // 简单配置 初始化设置 $smarty->ttemplatedir('./views'); $smarty->tcompiledir('./runtime/compile'); $smarty->tconfigdir('./config'); $smarty->addpluginsdir('./plugins'); $smarty->tcachedir('./runtime/cache'); $smarty->caching = 1;//开启缓存 $smarty->tcachelifetime(60*60*24); $smarty->left_delimiter = '{'; $smarty->right_delimiter = '}'; // 缓存机制 if (!$smarty->iscached('extends.html', $_rver['request_uri'])) { // 数据库查询 $data = [[]]; // 使用 $smarty->assign([ 'urname' => 'test-data', 'age' => 20 ]); // 数组 $smarty->assign('arr1', [1, 2, 3]); $smarty->assign('arr2', ['id' => 1, 'urname' => 'zhangsan', 'age' => 30]); $smarty->assign('urs', [ 体现汉字特点的古诗有哪些 ['id' => 1, 'urname' => 'zhangsan', 'age' => 30], ['id' => 2, 'urname' => 'lisi', 'age' => 40] ]); $smarty->assign('hobby_ids', [1, 2, 3]); $smarty->assign('hobby_output', ['看书', '敲代码', '看视频']); $smarty->assign('options', [ 1 => '看书', 2 => '敲代码', 3 => '看视频' ]); // 注册function $smarty->registerplugin('function', 'font', function ($attributes) { $text = $attributes['text']; $color = $attributes['color'] ?? 'black'; return '<span style="color: ' . $color . '">' . $text . '</span>'; }); // 注册变量修饰器 $smarty->registerplugin('modifier', 'link', function ($text, $href, $iscapitalize = fal) { $return = '<a href="' . $href . '">' . $text . '</a>'; if ($iscapitalize) { return ucwords($return); } return $return; }); // 注册块状函数 $smarty->registerplugin('block', 'link', function ($attributes, $text) { $href = $attributes['href']; if (!is_null($text)) { return '<a href="' . $href . '">' . $text . '</a>'; } }); // 对象 $smarty->assign('obj', $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'); $smarty->assign('urs', $data); } $smarty->clearcache('extends.html', $_rver['request_uri']); $smarty->clearallcache(); $smarty->display('extends.html', $_rver['request_uri']);
前端开发部分演示
注释和变量的使用smarty.html
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>title</title></head><body> <h2> <!-- 模板注释被*星号包围,而两边的星号又被定界符包围 --> {*$urname*} {$urname} </h2> <h3> <!-- 变量 --> {$age} </h3> <hr> <!-- 索引数组 --> arr1: {$arr1[1]} <hr> <!-- 关联数组 --> arr2: {$arr2['urname']} {$arr2.urname} <hr> <!-- 对象 --> object: {var_dump($obj->gettemplatedir())} <hr> <!-- 变量的运算 --> {$var = 100} {$var} {$foo = $var + 200} {$foo} <hr> {$foo} <hr> <!-- 保留变量的使用 --> $_get: {var_dump($smarty.get)} <hr> $_post: {var_dump($smarty.post)} <hr> $_request: {var_dump($smarty.request)} <hr> cookie: {var_dump($smarty.cookies)} <hr> ssion: {var_dump($smarty.ssion)} <hr> rver: {var_dump($smarty.rver)} <hr> env: {var_dump($smarty.env)} <hr> {time()} {$smarty.now} <hr> <!-- 加载配置文件后,配置文件中的变量需要用两个井号“#”包围或者是 smarty的保留变量$smarty.config.来调用 --> {config_load file='ba.conf'} {#font_size#} {$smarty.config.font_color}</body></html>
流程控制的使用loop.html
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>流程控制</title></head><body> <!-- 使用{if}处理分支结构 --> {$number = 200} {if $number === 100} gt {el if $number == 200} this number is 200 {el} this number isanywlan not 100 {/if} {$bool = fal} {if not $bool} not {/if} {if $number is not even} odd {/if} {if $number is not odd} even {/if} {if $number mod 2 == 0} even {/if} {if $number is not odd by 3} odd {/if} <!-- 使用{for}处理循环 --> {for $i = 5 to 4 step 2} {$i} {forel} no loop {/for} <!-- 使用{while}处理循环 --> {while $number > 195} {$number--} {/while} <!-- 使用{foreach}遍历数组 --> {foreach $arr2 as $key => $val} {if $val@first} {*break*} {continue} {/if} {$key}:{$val} {$val@key} {$val@index} {$val@iteration} {$val@first} {$val@last} {$val@show} {$val@total} {foreachel} data does not exist {/foreach} <!-- 使用{ction}遍历数组 --> {ction name=key loop=$arr1} {$arr1[key]} {/ction} {ction name=key loop=$urs2 step=-1 max=2} id: {$urs[key].id} urname: {$urs[key].urname} age: {$urs[key].age} {$smarty.ction.key.index} {$smarty.ction.key.iteration} {$smarty.ction.key.rownum} {$smarty.ction.key.index_prev} {$smarty.ction.key.index_next} {ctionel} no loop {/ction}</body></html>
常用标签函数的使用single_tag_func.html
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>常用标签函数的使用</title></head><body> {assign var="name" value="jason"} {assign "name" "jason lee"} {$name} {append var="arr1" value=4 index="3"} {var_dump($arr1)} {ldelim}$name{rdelim} {html_checkboxes name="hobby" values=$hobby_ids output=$hobby_output lected=$hobby_ids} {html_checkboxes name="hobby" options=$options lected=$hobby_ids} {html_image width="50" height="50" alt="google" href="http://www.google.com" file="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"} {html_options name="hobby" values=$hobby_ids output=$hobby_output lected=2} {html_options name="hobby" options=$options lected=2} {html_radios name="hobby" options=$options lected=2} {html_lect_date} {html_lect_time} {html_table loop=$arr1 cols=2 rows=3} {mailto address="86267659@qq.com" subject="test" text="给我发邮件" cc="123123@qq.com"} {math equation="x + y" x = 100 y = 200}</body></html>
变量修饰器的使用modifier.html
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>变量修饰器的使用</title></head><body> {$str="123123\nthis is string."} <hr> {$str|capitalize:true:true} <hr> {$str|capitalize:true:true|cat:'.'} <hr> {$str|count_characters} <hr> {$str|count_paragraphs} <hr> {$str|count_ntences} <hr> {$str|count_words} <hr> {$str2|default:'not data yet'} <hr> {time()|date_format:'%y-%m-%d %h:%m:%s'} <hr> {$chine = '中文'} {$chine|from_chart:'utf-8'|to_chart:'gb2312'} <hr> {$str|indent:10:'---'} <hr> {$str|lower|upper} <hr> {$str2="this is p1.\nthis is p2."} {$str2|nl2br} <hr> {$str|regex_replace:'/\d+/':' '} <hr> {$str|replace:'123123':'000'} <hr> {$str|spacify:'-'} <hr> {$float='10.0020398475'} {$float|string_format:'%.2f'} <hr> {$str3='a b c'} 挥汗如雨什么意思 {$str3|strip:'-'} <hr> {$tag='<b>font</b>'} {$tag|strip_tags} <hr> {$bigstr='123123123123123123ahjfdashfksdhfkjsdhjkfshfjkhsd'} {$bigstr|truncate:10:'---':true:true} <hr> {$tag|escape|unescape} <hr> {$bigstr|wordwrap:10:"\n":true}</body></html>
块函数的使用block_func.html
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>块函数的使用</title></head><body> {textformat indent='4' indent_first='10' indent_char='-' wrap='10' wrap_char='<hr>' wrap_cut=true assign='var'} aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa {/textformat} {*$var*} {nocache} {time()} {/nocache} <hr> {time()}</body></html>
插件的开发plugins.html
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>title</title></head><body> {font text=$text color='#123123'} {$text|link:'http://www.baidu.com'} {link href='http://www.baidu.com'} aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa {/link}</body></html>
smarty模板引擎插件的开发:
1、使用registerplugin( )方法扩充插件格式
2、在smarty模板的libs/plugins/目录下创建函数插件文件
block.link.php
function smarty_block_link($attributes, $text){ $href = $attributes['href']; if (!is_null($text)) { return '<a href="' . $href . '">' . $text . '</a>'; }}
function.font.php
function smarty_function_元旦节手抄报 简单font($attributes){ $text = $attributes['text']; $color = $attributes['color'] ?? 'black'; return '<span style="color: ' . $color . '">' . $text . '</span>';}
modifier.link.php
function smarty_modifier_link($text, $href, $iscapitalize = fal){ $return = '<a href="' . $href . '">' . $text . '</a>'; if ($iscapitalize) { return ucwords($return); } return $return;}
模板继承的使用
extends.html
!-- 使用{extends}函数实现模板继承合并子模板和父模板的{block}标签内容 -->{extends file="layout.html"}{block name="title"} article {$smarty.block.parent}{/block}{block name="content"} article list {$smarty.get.page} {*nocache*} {time()} {*/nocache*} {time()|date_format:'%h:%m:%s' nocache}{/block}
layout.html
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <title>{block name="title"} - imooc{/block}</title></head><body> <header> menu </header> {block name="content"}{/block} <footer> copyright </footer></body></html>
缓存机制
开启缓存
$smarty -> caching = 1|2|0;
$smarty -> tcachedir(“./cache”);
$smarty->tcachelifetime(300); // 5分钟,以秒为单位,-1永不过期
$smarty -> display(‘index.tpl’);
$smarty -> display(‘index.tpl’, $_rver[‘request_uri’]);
相关函数
iscached()
clearcache()
clearallcache()
本文发布于:2023-04-08 03:14:15,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/71c5d9bb6be447e5b03f7e98170c4396.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:MVC开发模式以及Smarty模板引擎的使用.doc
本文 PDF 下载地址:MVC开发模式以及Smarty模板引擎的使用.pdf
留言与评论(共有 0 条评论) |