PHP 模板smarty练习
一.练习环境javascript
smarty_inc为smarty lib库
smarty_inc.php导入库文件php
<?php include_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.phphtml
<?php include ("smarty_inc.php"); error_reporting(7);//不显示警告报错,255列出全部提示,0关闭全部提示 $str = 'this is my home!'; $smarty->assign('str',$str); $smarty->display("index.html"); ?>
index.htmljava
<!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!api
三.smarty数组练习
index.php数组
<?php include ("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.phpmarkdown
<?php include ("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.htmlide
<!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函数
<?php include ("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:' '}<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>
六.smarty内置函数练习
1.有键值数组
index.php
<?php include ("smarty_inc.php"); error_reporting(7);//不显示警告报错,255列出全部提示,0关闭全部提示 $arr_str = ['teacher1'=>'sq','teacher2'=>'ly','teacher3'=>'yxx']; $smarty->assign('teacher_name',$arr_str); $smarty->display("index.html");
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>smarty test</title> </head> <body> {foreach from=$teacher_name item=name key=teach} 数组内容:{$teach} - {$name}<br> {/foreach} </body> </html>
2.无键值数组
index.php
<?php include ("smarty_inc.php"); error_reporting(7);//不显示警告报错,255列出全部提示,0关闭全部提示 $arr_str = ['sq','ly','yxx']; $smarty->assign('teacher_name',$arr_str); $smarty->display("index.html");
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>smarty test</title> </head> <body> {foreach from=$teacher_name item=name key=teach} 数组内容:{$teach} - {$name}<br> {/foreach} </body> </html>
index.php
<?php include ("smarty_inc.php"); error_reporting(7);//不显示警告报错,255列出全部提示,0关闭全部提示 $arr_str = ['sq','ly','yxx']; $smarty->assign('teacher_name',$arr_str); $smarty->display("index.html");
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>smarty test</title> </head> <body> {foreach from=$teacher_name item=name} 数组内容:{$name}<br> {/foreach} </body> </html>
3.if条件语句
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>smarty test</title> </head> <body> 条件语句: {if name==''} 没有设置内容 {else} 有设置内容 {/if} </body> </html>
由于index.php没有设置这个变量,全部显示没有设置内容
4.include的使用
在模板下建立一个拼接文件head.html
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>smarty test</title> </head> <body> {include file='head.html'} 条件语句: {if name==''} 没有设置内容 {else} 有设置内容 {/if} </body> </html>
注:
{include file=‘d:\www\index2.php’} 能够是别的目录下的引入文件
{include file='head.html' title=‘menu’}引入head.html,将title的值传给head.html的中的变量title
5.literal的使用
literal数据将被看成文本处理,此时模板将忽略其内部的全部字符信息,该特性用于显示有可能包含大括号等字符信息的javascript脚本,由于在模板设置中php的边界设定为大括号,防止冲突。
{literal} <script language=javascript> ...... </script> {/literal}
6.Strip的使用strip标记中数据的首尾空格和回车,这样能够保证模板容易理解且不用担忧多余的空格致使问题,使用会作整行的处理,但内容不变,同时节省了流量,还能够保护代码。使用时在html代码 头和尾{strip}<html></html>{/strip}