温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何进行smarty的快速入门

发布时间:2021-11-26 09:29:34 来源:亿速云 阅读:192 作者:柒染 栏目:开发技术

这篇文章将为大家详细讲解有关如何进行smarty的快速入门,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

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'}

<h4>发布新闻</h4>

<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;}

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

{/literal}

</style>

</head>

<body>

<center>

{include file='menu.html'}

{block name='content'}

<h4>浏览新闻</h4>

<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>

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

<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的快速入门就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI