码迷,mamicode.com
首页 > 其他好文 > 详细

smarty 快速入门

时间:2018-01-31 22:20:51      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:fetch   sele   转换   style   sqli   die   temp   模板   user   

smarty 快速入门

 

smarty

定义:一个开源的模板引擎

模板引擎是为了使用户界面与业务数据分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档。

功能

将网站的数据和网站的界面实现分离(php和html代码)

缓存页面

下载

www.smarty.net

使用

1.引入smarty类库

2.实例化smarty对象

3.初始化参数

template_dir  模板存放目录

compile_dir  编译目录

4.分配变量

5.解析模板

注释    {* 这是注释的内容*}

忽略smarty解析     {literal} {/literal}

例子:

//第一步移入smarty类

require ‘./libs/Smarty.class.php‘;

 

//第二步实例化对象

$s = new Smarty;

 

//第三步初始化

$s->template_dir = ‘./View‘;

$s->compile_dir = ‘./View_c‘;

 

$pdo = new PDO(‘mysql:host=localhost;dbname=pass;charset=utf8‘,‘root‘,‘‘);

 

$stmt = $pdo->query(‘select * from news‘);

 

$res = $stmt->fetchAll(PDO::FETCH_ASSOC);

 

//var_dump($res);

 

//第四步 分配变量

$s->assign(‘title‘,‘新闻管理系统???????????‘);

// $s->assign(‘name‘,‘什么呢????‘);

 

$s->assign(‘res‘,$res);

 

 

//第五步 解析模板

$s->display(‘add.html‘);

    //建立 view文件存放改变网页 view_c转换文件

 

add.html

 

{extends file=‘index.html‘}

 

{block name=‘title‘}

<title>新闻添加页面</title>

{/block}

 

{block name=‘content‘}

 

<h3>发布新闻</h3>

<form action="action.php?action=add" method=‘post‘>

<table border=‘0‘ width=‘400‘> 

<tr>

<td align=‘right‘>标题:</td>

<td><input type="text" name=‘title‘></td>

</tr>

<tr>

<td align=‘right‘>关键字:</td>

<td><input type="text" name=‘keywords‘></td>

</tr>

<tr>

<td align=‘right‘>作者:</td>

<td><input type="text" name=‘author‘></td>

</tr>

<tr>

<td align=‘right‘>内容:</td>

<td><textarea name="content" id="" cols="30" rows="5" width=‘300px‘ height=‘200px‘ style=‘resize:none‘></textarea></td>

</tr>

<tr>

<td colspan=‘3‘ align=‘center‘>

<input type="submit" value=‘添加‘ />&nbsp;&nbsp;&nbsp;

<input type="reset" value=‘重置‘ />

</td>

 

</tr>

 

</table>

</form>

 

{/block}

 

index.html

 

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

{block name=‘title‘}

<title>新闻管理系统</title>

{/block}

<style type="text/css">

{literal}

table,td{font-family:微软雅黑;text-align:center;}

h3{font-family:微软雅黑;}

{/literal}

</style>

</head>

<body>

<center>

{include file=‘menu.html‘}

 

{block name=‘content‘}

<h3>浏览新闻</h3>

 

<table border=‘1‘ width=‘880‘>

<tr>

<th>新闻ID</th><th>新闻标题</th><th>新闻关键字</th><th>作者</th><th>新闻内容</th><th>操作</th>

</tr>

 

{foreach $res as $v}

 

<tr>

<td><?= $v[‘id‘]?></td>

<td><?= $v[‘title‘]?></td>

<td><?= $v[‘price‘]?></td>

<td><?= $v[‘url‘]?></td>

 

</tr>

 

{/foreach}

 

</table>

{/block}

 

 

</center>

</body>

</html>

 

menu.html

 

<meta charset=‘utf-8‘ />

<style type="text/css">

body{ font-family:微软雅黑;}

</style>

<h2>新闻管理系统</h2>

<a href="./index.php">浏览新闻</a>  | 

<a href="./add.php">发布新闻</a>

<hr width=‘800px‘ />

 

 

##smarty
    //第一步移入smarty类
    require ‘./libs/Smarty.class.php‘;
    //第二部实例化对象
    $s = new Smarty;
    //第三部初始化
    //模版目录初始化,模版存放目录
    $s->template_dir = ‘./View‘;
    //编译目录
    $s->compile_dir = ‘./View_c‘;
    $pdo = new PDO(‘mysql:host=localhost;dbname = pass;charset = utf8‘,‘root‘,‘‘);
    $stmt = $pdo->query(‘select * from stu‘);
    $res = $stmt->fetchAll(PDO::FETCH_ASSOC);

    //第四步分配变量
    $s->assign(‘title‘,‘smart的一个模版‘);
    $s->assing(‘name‘,‘mingzi‘);
    $s->assing(‘res‘,‘$res‘);
    //第五步解析模版
     $s->display(‘2.html‘);

     2.html更改
    {$ title} 
    {$name}
    {foreach $res as $v}
        {$v[‘id‘]}
        {$v[‘name‘]}

    {/foreach}
    {literal}

    {/literal}
    {include file=‘menu.html‘}  引入
    
    {block name =‘content‘}s
    mysqli 
    //引入文件/
    define(‘HOST‘,‘localhost‘);
    define(‘USER‘,‘root‘);
    define(‘PWD‘,‘‘);
    define(‘DBNAME‘,‘pass‘);
    define(‘UTF‘,‘utf8‘);


    $link = @mysqli_connect(HOST,USER,PWD) or die(‘连接失败‘)
    mysqli_select_db($link,DBNAME);  //选择数据库
    mysqli_set_charset($link,UTF);    //字符集
    $sql = ‘select * from news order by id‘);
    $result = mysqli_query($link,$sql);
    //查询结果辅助函数
    mysqli_num_rows($result)>0
    mysqli_fetch_assoc($result)   得到关联数组
    mysqli_close($link)  //关闭数据库 
         
    

 

smarty 快速入门

标签:fetch   sele   转换   style   sqli   die   temp   模板   user   

原文地址:https://www.cnblogs.com/haibo-py/p/8393965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!