smarty 模板if else使用实例与入门教程

 更新时间:2016年11月25日 15:57  点击:1348
标签是给Smarty的指令符,以模板定界符包住。这些指令符可以是变量,以$符号代表函数、逻辑 或 流程控制 语法。Smarty 允许 PHP 程式设计师以Smarty 标签去定义可存取的函数

smarty 模板if else使用实例与入门教程
smarty 主要功能是完成美工与程序分离,并且使用的缓存技术,减轻服务器的负载,
smarty 以在文件中放置特殊的“Smarty标签”来产生网页内容。这些标签会被处理并替换成其他的内容。

 

*/

require('smarty/libs/Smarty.class.php教程');

$tpl = new Smarty();
$tpl->template_dir   = 'd/'
$tpl->compile_dir    = 'd/template_c';

$tpl->compile_check   = false;//$cfg['debug'];
$tpl->debugging      = false;
$tpl->caching       = false;
$tpl->cache_lifetime  = 3600;

$tpl->left_delimiter  = '<!--{';
$tpl->right_delimiter  = '}-->';

$array = 'www.111cn.net'
$tpl->assign('dataList', $array);
$tpl->display('a.tpl');

/*
a.tpl
<!--{if $dataList =='www.111cn.net'}-->
 <!--{$dataList}-->
<!--{else}-->
 echo 'aaa';
<!--{/if}-->

还有另一种方法

{if $name eq 'Fred'}
    Welcome Sir.
{elseif $name eq 'Wilma'}
    Welcome Ma'am.
{else}
    Welcome, whatever you are.
{/if}

{* an example with "or" logic *}
{if $name eq 'Fred' or $name eq 'Wilma'}
   ...
{/if}

本文章原创于www.111cn.net转载注明出处

unction explode_content($content, $length) { 

02 $i = 0; 

03 $k = 1; 

04 $j = 0; 

05 $wn = 0; 

06 $s = ''; 

07 $e = 1; 

08 $yh = 0; 

09 while ($k) { 

10 $d = $content[$i]; 

11 if ($d !== '') { 

12 if (ord($d) > 127) { 

13 $j++; 

14 $num = 2; 

15 $i++; 

16 } else { 

17 $num = 1; 

18 } 

19 $j++; 

20 } else { 

21 if ($s) { 

22 $listdb[] = addslashes($s); 

23 } 

24 $k = 0; 

25 } 

26 $v1 = $j - $num; 

27 $w = substr($content, $v1, $num); 

28 if ($w !== '') { 

29   

30 if ($w == '<') { 

31 $e = 0; 

32 } 

33 if (!$e && $w == '"') { 

34 $yh++; 

35 } 

36 if ($e && $w != ' ' && $w != ' ') { 

37 $wn++; 

38 } 

39 if ($w == '>' && $yh % 2 == 0) { 

40 $e = 1; 

41 } 

42 $s .= $w; 

43 } 

44 if ($wn >= $length && $e) { 

45 $listdb[] = addslashes($s); 

46 $s = ''; 

47 $wn = 0; 

48 } 

49 $i++; 

50 } 

51 return $listdb; 

52 } 

53   

54   

55   

56 分页使用 

57 $page_per = 500; //每页字数 
php文章内容分页
58 $article_list = explode_content($article, $page_per); //拆分文章 

59 $page_count = count($article_list); 

60 $page = !empty($_GET['page']) ? intval($_GET['page']) : 1; 

61 $page = ($page > $page_count && $page_count>0) ? $page_count : $page; 

62 $article = strips教程lashes($article_list[$page -1]); 

63 if ($page_count>1) { 

64 if ($page == 1) { 

65 $page_code = "<a href="news.php?id=$id&page=".($page+1)."">下页</a> <a href="news.php?id=$id&page=$page_count">末页</a> "; 

66 } 

67 elseif ($page == $page_count) { 

68 $page_code = "<a href="news.php?id=$id&page=".($page-1)."">上页</a> <a href="news.php?id=$id&page=1">首页</a> "; 

69 } else { 

70 $page_code = "<a href="news.php?id=$id&page=".($page+1)."">下页</a> <a href="news.php?id=$id&page=".($page-1)."">上页</a> "; 

71 } 

72 }

修改dedecms 织梦系统 生成静态页面栏目缓存文件路径
由于dedecms 生成频道栏目,要生成一个临时mkall_cache_{adminid}.php教程文件,这对网站做安全会有一定的影响,特别我现在是把我整个网站限制不能上传php,js文件,为了方便起见,今天我就来拿我修改过程记录下来与各位分享吧。
首页我们找到dedecms  的后台管理上当默认是在dede/目录。

找到文件:
makehtml_all.php
找到73行,

//$mkcachefile = DEDEROOT."/data/mkall_cache_{$adminID}.php";
改成你的站外目录如
$mkcachefile = "s:/s/data/mkall_cache_{$adminID}.php";

这是要写缓存文件的,就是把你更新的目录ID保存到一个数组里

再找到147行

//$mkcachefile = DEDEDATA."/mkall_cache_{$adminID}.php";
$mkcachefile = "s:/s/data/mkall_cache_{$adminID}.php";

好了,保存文件,我们再在当前上当找到

makehtml_list_action.php

找到第37行
//$mkcachefile = DEDEROOT."/data/mkall_cache_{$adminID}.php";
$mkcachefile = "s:/s/data/mkall_cache_{$adminID}.php";

保存上传就OK了。

本文章原创于www.111cn.net 转载注明出处

<?php教程
class resizeimage
{
    //图片类型
    var $type;
    //实际宽度
    var $width;
    //实际高度
    var $height;
    //改变后的宽度
    var $resize_width;
    //改变后的高度
    var $resize_height;
    //是否裁图
    var $cut;
    //源图象
    var $srcimg;
    //目标图象地址
    var $dstimg;
    //临时创建的图象
    var $im;
    function resizeimage($img, $wid, $hei,$c,$dstpath)
    {
        $this->srcimg = $img;
        $this->resize_width = $wid;
        $this->resize_height = $hei;
        $this->cut = $c;
        //图片的类型
        $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
        //初始化图象
        $this->initi_img();
        //目标图象地址
        $this -> dst_img($dstpath);
        //--
        $this->width = imagesx($this->im);
        $this->height = imagesy($this->im);
        //生成图象
        $this->newimg();
        ImageDestroy ($this->im);
    }
    function newimg()
    {
        //改变后的图象的比例
        $resize_ratio = ($this->resize_width)/($this->resize_height);
        //实际图象的比例
        $ratio = ($this->width)/($this->height);
        if(($this->cut)=="1")
        //裁图
        {
            if($ratio>=$resize_ratio)
            //高度优先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_ratio)
            //宽度优先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
        else
        //不裁图
        {
            if($ratio>=$resize_ratio)
            {
                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_ratio)
            {
                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
    }
    //初始化图象
    function initi_img()
    {
        if($this->type=="jpg")
        {
            $this->im = imagecreatefromjpeg($this->srcimg);
        }
        if($this->type=="gif")
        {
            $this->im = imagecreatefromgif($this->srcimg);
        }
        if($this->type=="png")
        {
            $this->im = imagecreatefrompng($this->srcimg);
        }
    }
    //图象目标地址
    function dst_img($dstpath)
    {
        $full_length  = strlen($this->srcimg);
        $type_length  = strlen($this->type);
        $name_length  = $full_length-$type_length;
        $name         = substr($this->srcimg,0,$name_length-1);
        $this->dstimg = $dstpath;
//echo $this->dstimg;
    }
}
$resizeimage = new resizeimage("1.jpg", "200", "150", "1","2.jpg");
?>

传值方法很多,
参数传值: 可以是urs.php?id=1带参数形式,这是页面之间比较主要的传值方式 ,用request,get 接收值.
from表单传值 : 主要接收request传值 post, get  来接收,from标签可以选择get或者post 通过url传值的是get 还可以利用ajax传值可以选择post或者get.

session传值 : 这个一般是做用户登陆时用的,服务器全局变量,一般不用在页面之前的传值
cookie传值 :把内容保存在客户端,

我们常用的页面传值主要是参数传值  from表单传值传值。

现在来做几个实例
*/
//先用页面参数传值实例

?>

<a href="b.php?action=1">点击我</a>
//输出值为 1

用form传值

<form action="b.php" method="get"> 

<input name="action" type="text" value="5">

<input type="submit" value="submit" /> 

</form>

得出值 5

<form action="b.php" method="POST"> 

<input name="action" type="text" value="5">

<input type="submit" value="submit" /> 

</form>


<?
if( $_GET )
{
 echo 'get传值',$_GET['action'];
}
else
{
 echo 'post传值', $_POST['action'];
}
//本文章原创于www.111cn.net 中国WEB第一站,,转载注明出处

[!--infotagslink--]

相关文章

  • 图解PHP使用Zend Guard 6.0加密方法教程

    有时为了网站安全和版权问题,会对自己写的php源码进行加密,在php加密技术上最常用的是zend公司的zend guard 加密软件,现在我们来图文讲解一下。 下面就简单说说如何...2016-11-25
  • ps怎么使用HSL面板

    ps软件是现在很多人都会使用到的,HSL面板在ps软件中又有着非常独特的作用。这次文章就给大家介绍下ps怎么使用HSL面板,还不知道使用方法的下面一起来看看。 &#8195;...2017-07-06
  • Plesk控制面板新手使用手册总结

    许多的朋友对于Plesk控制面板应用不是非常的了解特别是英文版的Plesk控制面板,在这里小编整理了一些关于Plesk控制面板常用的使用方案整理,具体如下。 本文基于Linu...2016-10-10
  • 使用insertAfter()方法在现有元素后添加一个新元素

    复制代码 代码如下: //在现有元素后添加一个新元素 function insertAfter(newElement, targetElement){ var parent = targetElement.parentNode; if (parent.lastChild == targetElement){ parent.appendChild(newEl...2014-05-31
  • 使用GruntJS构建Web程序之构建篇

    大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
  • 使用percona-toolkit操作MySQL的实用命令小结

    1.pt-archiver 功能介绍: 将mysql数据库中表的记录归档到另外一个表或者文件 用法介绍: pt-archiver [OPTION...] --source DSN --where WHERE 这个工具只是归档旧的数据,不会对线上数据的OLTP查询造成太大影响,你可以将...2015-11-24
  • 如何使用php脚本给html中引用的js和css路径打上版本号

    在搜索引擎中搜索关键字.htaccess 缓存,你可以搜索到很多关于设置网站文件缓存的教程,通过设置可以将css、js等不太经常更新的文件缓存在浏览器端,这样访客每次访问你的网站的时候,浏览器就可以从浏览器的缓存中获取css、...2015-11-24
  • jQuery 1.9使用$.support替代$.browser的使用方法

    jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support 。 在更新的 2.0 版本中,将不再支持 IE 6/7/8。 以后,如果用户需要支持 IE 6/7/8,只能使用 jQuery 1.9。 如果要全面支持 IE,并混合...2014-05-31
  • Lua语言新手简单入门教程

    这篇文章主要给大家介绍的是关于Lua语言新手入门的简单教程,文中通过示例代码一步步介绍的非常详细,对各位新手们的入门提供了一个很方便的教程,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。...2020-06-30
  • 安装和使用percona-toolkit来辅助操作MySQL的基本教程

    一、percona-toolkit简介 percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括: 检查master和slave数据的一致性 有效地对记录进行归档 查找重复的索...2015-11-24
  • C#注释的一些使用方法浅谈

    C#注释的一些使用方法浅谈,需要的朋友可以参考一下...2020-06-25
  • MySQL日志分析软件mysqlsla的安装和使用教程

    一、下载 mysqlsla [root@localhost tmp]# wget http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz--19:45:45-- http://hackmysql.com/scripts/mysqlsla-2.03.tar.gzResolving hackmysql.com... 64.13.232.157Conn...2015-11-24
  • php语言中使用json的技巧及json的实现代码详解

    目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它。我写过一篇《数据类型和JSON格式》,探讨它的设计思想。今天,我想总结一下PHP语言对它的支持,这是开发互联网应用程序(特别是编写API)必须了解的知识...2015-10-30
  • 深入解析WordPress中加载模板的get_template_part函数

    这篇文章主要介绍了WordPress中加载模板的get_template_part函数,其中重点讲解了其函数钩子的使用,需要的朋友可以参考下...2016-01-14
  • PHP实现无限级分类(不使用递归)

    无限级分类在开发中经常使用,例如:部门结构、文章分类。无限级分类的难点在于“输出”和“查询”,例如 将文章分类输出为<ul>列表形式; 查找分类A下面所有分类包含的文章。1.实现原理 几种常见的实现方法,各有利弊。其中...2015-10-23
  • php类的使用实例教程

    php类的使用实例教程 <?php /** * Class program for yinghua05-2 * designer :songsong */ class Template { var $tpl_vars; var $tpl_path; var $_deb...2016-11-25
  • 双冒号 ::在PHP中的使用情况

    前几天在百度知道里面看到有人问PHP中双冒号::的用法,当时给他的回答比较简洁因为手机打字不大方便!今天突然想起来,所以在这里总结一下我遇到的双冒号::在PHP中使用的情况!双冒号操作符即作用域限定操作符Scope Resoluti...2015-11-08
  • mysql配置模板(my-*.cnf)参数详细说明

    mysql安装成功后有几个默认的配置模板,列表如下: my-huge.cnf : 用于高端产品服务器,包括1到2GB RAM,主要运行mysql my-innodb-heavy-4G.ini : 用于只有innodb的安装,最多有4GB RAM,支持大的查询和低流量 my-large.cnf : 用于...2015-03-15
  • PHP mysql与mysqli事务使用说明 分享

    mysqli封装了诸如事务等一些高级操作,同时封装了DB操作过程中的很多可用的方法。应用比较多的地方是 mysqli的事务。...2013-10-02
  • Postman安装与使用详细教程 附postman离线安装包

    这篇文章主要介绍了Postman安装与使用详细教程 附postman离线安装包,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-05