smarty的学习计划(1)

1.什么事smarty?

不知道,smarty是一个使用PHP写出来的模板引擎,它提供了逻辑外在内容的分离

2.smarty优点:

a.速度:成熟的模板引擎技术

b.编译型:采用smarty编写的程序运行时要编译成一个非模板技术的PHP文件,这个文件采用了PHP与HTML混合的方式,在下一次访问模板时将web
请求直接转换到这个文件中,而不再进行模板重新编译

c.缓存技术:smarty选用的一种缓存技术,她可以将用户最终看到的HTML文件缓存成一个静态HTML页面,当设定smarty的cache属性为true时,

在smarty设定的cachetime期内用户的web请求直接转换到这个静态HTML文件中来,这相当于调用一个静态的HTML文件

d.插件技术:smarty可以自定义插件。插件实际就是一些自定义的函数

模板中可以使用if/elseif/else/endif.在模板文件使用判断语句可以非常方便的对模板进行格式重排

3.smarty缺点:

a.需要实时更新的内容,需要经常对数据进行跟新,这类型程序使用smarty会使模板处理速度变慢

b.小项目

tpl 表示一个smarty的模板

实例:lib里建立tpl模板,如:eg01.tpl

代码:

<html>
<head><title>模板中内定的一些函数</title></head>
<body>

{*下面的这一段相当于在模板内部定义一个变量UserName*}
{assign var="UserName" value="大师兄"} 
这里将显示模板内部定义的一个变量:UserName = {$UserName}

下面的这一行将显示3个checkBox:<br>
{html_checkboxes name="CheckBox" values=$CheckName checked=$IsChecked output=$value separator="<br />"}
下面在这一行将显示3个radio:<br>
{html_radios name="RadioBox" values=$RadioName checked=$IsChecked output=$value separator="<br />"}


下面显示一个月,日, 年选择框:<br>
{html_select_date}

<hr><b>CopyRight(C) By XiaoJun, Li 2004<b>{mailto address="teacherli@163.ccom" text="联系作者"}

</body>
</html>

  

eg01.php:

<?php
require_once ("./comm/Smarty.class.php");

$smarty = new Smarty();
$smarty->template_dir = './templates/';
$smarty->compile_dir = './templates_c/';
$smarty->config_dir = './configs/';
$smarty->cache_dir = './cache/';
$smarty->caching = false;

//--------------------------------------------------------------------------------------
//处理{html_checkboxes name="CheckBox" values=$CheckName checked=$IsChecked output=$value separator="<br />"}
//--------------------------------------------------------------------------------------
$smarty->assign('CheckName', array(
1001 => '语文',
1002 => '数学',
1003 => '外语'));
$smarty->assign('IsChecked', 1001);


//--------------------------------------------------------------------------------------
//处理{html_radios name="RadioBox" values=$RadioName checked=$IsChecked output=$value separator="<br />"}
//--------------------------------------------------------------------------------------
$smarty->assign('RadioName', array(
1001 => '语文',
1002 => '数学',
1003 => '外语'));
$smarty->assign('IsChecked', 1001);

//--------------------------------------------------------------------------------------
//{html_select_date}不用处理会自动输出
//--------------------------------------------------------------------------------------

$smarty->display("example3.tpl");
?>

纯学习内容

smarty函数:

$smarty  = new Smarty();//简历实例对象

$smarty->template_dir = './templates' //设置模板目录

$smarty->compile_dir = './templates_c' //设置编译目录

$smarty->left_delimiter = '<{'; //定义左边界

$smarty->assign('name','杨玲');//进行模板变量替换

$smarty->assign('yang',array( //制定一维数组

a=>'A',

b='B',

c='C'));

//制定二维数组

$array[] = array('id'=>'001','title'=>'one');

$array[] = array('id'=>'002','title'=>'two');

$smarty->assign('news',$array);

 

$smarty->cache_dir = './cache/';//设置缓存目录

$smarty->caching = false;

$smarty->config_dir = './configs/';//设置配置文件目录

 

$smarty->display('eg01.tpl');//编译并显示位于./templates下的eg01.tpl模板

连接数据库

<?php
$db = mysql_connect("localhost","root","root")  or die('数据库连接错误');
mysql_select_db("News",$db);
//连接上数据库后,处理语句都是原生态php函数
$query = "select id,title from news_test  order by orderby_id";
$result = mysql_query($query);
while($row = myswl_fetch_array($result)){
$array[] = array('id'=>$row['id']),
                           'tilte'=>$row['title']  
}
$smarty = assign("News_A",$array);
unset($array);//注销,对$array赋值时,不会清空它,而是增加新元素
mysql_free_result();
mysql_close($db);

  比较常用的函数和基础知识在这了,继续学习

原文地址:https://www.cnblogs.com/linglingyang/p/3988981.html