摘要:1、配置文件<?php require './vendor/autoload.php'; $smarty = new Smarty(); $smarty->setTemplateDir('./temp'); $smarty->setCompileDir('./temp_c'); $smar
1、配置文件
<?php
require './vendor/autoload.php';
$smarty = new Smarty();
$smarty->setTemplateDir('./temp');
$smarty->setCompileDir('./temp_c');
$smarty->setCacheDir('./cache');
$smarty->setConfigDir('./config');
$smarty->setLeftDelimiter('<{');
$smarty->setRightDelimiter('}>');
$smarty->setCaching(false);
$smarty->setCacheLifetime(60*60*24);
//echo print_r($smarty->getTemplateDir(),true);
//echo '<hr>';
//echo $smarty->getCompileDir();
//echo '<hr>';
//echo $smarty->getCacheDir();
//echo '<hr>';
//echo print_r($smarty->getConfigDir(),true);2、PHP
<?php
session_start();
//加载Smart配置
require 'config/config.php';
//变量
$name = '中国';
$smarty->assign('names',$name);
//索引数组
$country = ['中国','俄罗斯','美国','加拿大','日本','韩国'];
$smarty->assign('country',$country);
//关联数组
$area = [
'first'=>'俄罗斯',
'second'=>'加拿大',
'third'=>'中国',
'fourth'=>'美国'
];
$smarty->assign('area',$area);
//二维数组
$price = [
['name'=>'西红柿','price'=>3],
['name'=>'黄瓜','price'=>5],
['name'=>'茄子','price'=>6]
];
$smarty->assign('price',$price);
//对象
class Test
{
public $site= 'www.baidu.com';
public function welcome ()
{
return '欢迎访问:' .$this->site;
}
}
$test = new Test();
$smarty->assign('test',$test);
function add($a,$b)
{
return $a + $b;
}
const SITE_NAME = 'BAIDU';
$_POST['user_name'] = 'admin';
$_GET['page'] = 5;
$_SESSION['pass'] = sha1(123456);
$smarty->display('book2.html');3、模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>book2</title>
</head>
<body>
<{* 显示变量 *}>
<h3>我来自:<span style="color: red;"><{$names}></span></h3>
<{* 显示索引数组 *}>
<p>国家有哪些:<{$country[0]}>,<{$country[1]}>,<{$country[2]}></p>
<{* 显示关联数组 *}>
<p>面积排行榜:</p>
<p>第一:-----><{$area.first}></p>
<p>第二:-----><{$area.second}></p>
<p>第三:-----><{$area.third}></p>
<p>第四:-----><{$area.fourth}></p>
<{* 显示二维数组 *}>
<p>蔬菜价格</p>
<p>品名:<{$price.0.name}> 价格:<{$price.0.price}></p>
<{* 显示对象 *}>
<p><{$test->site}></p>
<p><{$test->welcome()}></p>
<p><{$price.0.price + 10}></p>
<p><{str_replace('.','-',$test->site)}></p>
<p><{add(5,9)}></p>
<p><{$smarty.const.SITE_NAME}></p>
<{config_load file = "app.conf"}>
<p><{$smarty.config.app_name}></p>
<{config_load file = "app.conf" section = "db"}>
<p><{$smarty.config.dbname}></p>
<p><{$smarty.post.user_name}></p>
<p><{$smarty.get.page}></p>
<p><{$smarty.session.pass}></p>
</body>
</html>
批改老师:查无此人批改时间:2019-09-05 14:27:44
老师总结:完成的不错。php的框架多学几款,对以后工作有帮助。继续加油