PHP 模板smarty练习
一.练习环境
smarty_inc为smarty lib库
smarty_inc.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.php
<?php
include ("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
<?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.php
<?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.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
<?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>
原文地址:http://blog.51cto.com/anfishr/2144702
时间: 2024-11-17 03:56:07