分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

PHP 模板smarty练习

发布时间:2023-09-06 02:04责任编辑:顾先生关键词:PHP
PHP 模板smarty练习
一.练习环境

smarty_inc为smarty lib库
smarty_inc.php导入库文件

<?phpinclude_once ("smarty_inc/Smarty.class.php");$smarty = new Smarty(); ?//实例化$smarty->config_dir="Smarty/Config_File.class.php";$smarty->caching=false; ?//缓存$smarty->template_dir = "./templates"; ?//模板文件$smarty->compile_dir = "./templates_c"; // 设定编译文件的存储路径//$smarty->cache_dir = "./smarty_cache";$smarty->left_delimiter = "{";$smarty->right_delimiter = "}";?>

二.smarty变量练习
$smarty->assign 设置传输变量
$smarty->display 加载模板
index.php

<?phpinclude ("smarty_inc.php");error_reporting(7);//不显示警告报错,255列出所有提示,0关闭所有提示$str = ‘this is my home!‘;$smarty->assign(‘str‘,$str);$smarty->display("index.html");?>

index.html

<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>smarty test</title></head><body> ???{$str}</body></html>

访问localhost/smarty/index.php 直接转到index.html
this is my home!

三.smarty数组练习
index.php

<?phpinclude ("smarty_inc.php");error_reporting(7);//不显示警告报错,255列出所有提示,0关闭所有提示$students = [‘sunqing‘,‘liuyao‘,‘yuxx‘,‘王舞‘];$smarty->assign(‘name‘,$students);$smarty->display("index.html");

index.html

<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>smarty test</title></head><body> ???{section name=stu loop=$name} ???????{$name[stu]} ???{sectionelse} ???????无内容 ???{/section}</body></html>

四.smarty二维数组练习
index.php

<?phpinclude ("smarty_inc.php");error_reporting(7);//不显示警告报错,255列出所有提示,0关闭所有提示$students[] = [‘stu_name‘=>‘sunqiang‘];$students[] = [‘stu_name‘=>‘liuyao‘];$students[] = [‘stu_name‘=>‘yuxx‘];$smarty->assign(‘name‘,$students);$smarty->display("index.html");

index.html

<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>smarty test</title></head><body> ???{section name=stu loop=$name} ???????{$name[stu].stu_name} ???{sectionelse} ???????无内容 ???{/section}</body></html>

五.smarty标量操作符练习
index.php

<?phpinclude ("smarty_inc.php");error_reporting(7);//不显示警告报错,255列出所有提示,0关闭所有提示$str = ‘this is my home!‘;$smarty->assign(‘str‘,$str);$smarty->assign(‘time‘,time());$smarty->assign(‘score‘,123.456);$smarty->display("index.html");

index.html

<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>smarty test</title></head><body>原始字符:{$str}<br><hr>首字母大写:{$str|capitalize}<br>计算字符数:{$str|count_characters}<br>连接内容:{$str|cat:‘ i love study php‘}<br>段落数:{$str|count_paragraphs}<br> <!--回车计算段落数-->计算句数:{$str|count_sentences}<br>计算单词数:{$str|count_words}<br>日期显示:{$time|date_format:‘%Y-%m-%d %H:%M:%S‘} | {$smarty.now|date_format:‘%Y-%m-%d %H:%M:%S‘} | {$smarty.now}<br>默认显示值:{$str1|default:‘no content‘}<br>转码:{$str|escape:url} | {$str|escape:html}<br>缩进:{$str|indent:5:‘.‘} | {$str|indent:5:‘&nbsp;‘}<br>大写:{$str|upper}<br>小写:{$str|lower}<br>替换:{$str|replace:‘my‘:‘your‘} | {$str|replace:‘my‘:‘**‘}<br><!--屏蔽关键词-->字符串格式化:{$score|string_format:‘%.2f‘} | {$score|string_format:‘%d‘}<br><!--保留小数点2位,保留整数-->去空格:{$str|strip:‘‘} | {$str|strip:‘_‘}<br>去html标签:{$str|strip_tags}<br>截取:{$str|truncate:10:‘...‘}<br>行宽约束:{$str|wordwrap:10:‘<br>‘}</body></html>

PHP 模板smarty练习

原文地址:http://blog.51cto.com/anfishr/2144702

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved