0810 smarty

1.Smarty简介(why,what)

why:
smarrty 将前端工程师和程序员的工作分开,让前端工程师完成前台页面的工作,程序员完成后台的业务逻辑。
what
模版引擎是用来将PHP代码和模版页组合成PHP 混合页的PHP类

2.smarty版本

smarty-2.6.26.zip
smarty-stable-3.1.8.zip
internals
plugins 插件
config_file.class.php 配置文件
debug.tpl 调试类
smarty.class.php smarty类
smarty_compiler.class.php 编译类

3.快速入门

步骤:
创建2个目录:templates,模板目录;templates_c编译后的目录
实例化类,开始初始化(3行)

4.smarty执行步骤详解

①浏览器访问PHP程序
②PHP将模版引擎加载进来
③模版引擎将PHP中变量加载到引擎类
④模版引擎将美工模板文件(TPL)加载到引擎类
⑤模版引擎合成编译后的模版文件
⑥将合成好的模版文件导入回PHP程序
⑦由PHP文件输出最终效果
注意:
①判断如果存在编译后的文件(com)则不加载引擎而直接输出编译后的文件(com)
②判断TPL模版文件有没有被修改过,如果没有修改则直接输出,如果修改过则从步骤②开始执行编译

5.smarty详细初始化配置

<?php
//引入smarty类
include('libs/Smarty.class.php');
//初始化
$smarty = new Smarty();
$smarty->assign('aa','aaaaaaaaaaa');

//指定模板的目录
$smarty->template_dir = 'tpl';
//指定编译的目录
$smarty->compile_dir = 'com';

//定义坐标的{
$smarty->left_delimiter = '<{';
$smarty->right_delimiter = '}>';

//缓存
$smarty->caching = 1;
//1是开启,0是关闭

$smarty->cache_dir = 'caches';
$smarty->cache_lifetime = 10;

//插件
$smarty->plugins_dir = 'plugins';

//配置文件
$smarty->config_dir = 'configs';

//配置文件
$smarty->debugging = true;


//加载模板页
$smarty->display('index.html');
?>

6.smarty语法&控制流

7.模版中显示数据

直接显示
<legend>直接访问数组</legend>
显示索引数组:
{$arr_1[0]}
<br />
显示关联数组:
{$arr_2.aa}
<br />
显示二维数组

{$arr_3[2].cc}

foreach
{foreach from=$arr_4 item=val}
{foreach rom=$val item=v}
{$v}
{/foreach}<br />
{/foreach}

foreach($arr_4 as $val){
foreach($val as $v){
echo $v;
}
}

section
{section loop = $arr_4 name=aso}
<tr>
<td>{$arr_4[aso].name}</td>
<td>{$arr_4[aso].sex}</td>
<td>{$arr_4[aso].power}</td>
<td>{$arr_4[aso].skill}</td>
</tr>
{/section}

$arr_4 数组直接放到$arr[aso]

8.smarty&js整合

9.smarty&mysql整合

$db = new PDO('mysql:host=localhost;dbname=news;charset=UTF-8', 'root', '12345');

声明数据库类型 数据库地址/名称 数据库里的具体库名 用户名 密码
$db->query();vs db->exec();区别
//query返回有结果集的,没有结果集的用
$db->fetchAll(); //所以记录
$db->fetch();//单行记录
$db->rowCount();//有几条记录
$db->lastInsertId();//最后一条插入的id

匈牙利命名法:fetch_all()
驼峰命名法: fetchAll
10.smarty&MVC整合

原文地址:https://www.cnblogs.com/suihui/p/3169765.html