smarty简单应用实例

本文讲述了smarty简单应用实例。分享给大家供大家参考,具体如下:

<?php
require 'smarty/libs/Smarty.class.php';
$smarty = new Smarty;
$smarty->template_dir="smarty/templates/templates";
$smarty->compile_dir="smarty/templates/templates_c";
$smarty->config_dir="smarty/templates/config";
$smarty->cache_dir="smarty/templates/cache";
$hello = "Hello World!";
$smarty->compile_check = true;
//$smarty->debugging = true;
$smarty->debugging = false;
$smarty->caching=true;
$conn=mysql_connect("localhost", "root","root"); //打开MySQL服务器连接
mysql_select_db("lava_guess"); //链接数据库
mysql_query("set names utf8"); //解决中文乱码问题
$sql_list="Select * from t_sys_msg Order by id desc limit 0,10";
$result_list=mysql_query($sql_list); //执行sql语句,返回结果
//把记录集转换为数组
while($rs_list=mysql_fetch_array($result_list))
{
   $msg_array[]=$rs_list;
}
$array[] = array("newsID"=>1, "newsTitle"=>"第1条新闻");
$array[] = array("newsID"=>2, "newsTitle"=>"第2条新闻");
$array[] = array("newsID"=>3, "newsTitle"=>"第3条新闻");
$array[] = array("newsID"=>4, "newsTitle"=>"第4条新闻");
$array[] = array("newsID"=>5, "newsTitle"=>"第5条新闻");
$array[] = array("newsID"=>6, "newsTitle"=>"第6条新闻");
$smarty->assign("newsArray", $array);
$smarty->assign("hello",$hello);
//赋值
$smarty->assign("msglist",$msg_array);
$smarty->display('index.tpl');
?>

模板文件:index.tpl

<html>
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Smarty</title></head>
<body>
<hr>
{$hello}
<hr>
 {section name=loop loop=$msglist}
 id: {$msglist[loop].id}--{$msglist[loop].title}----{$msglist[loop].receiver}<br>
 {/section}
<hr>
{section name=loop loop=$newsArray}
新闻编号:{$newsArray[loop].newsID}<br>
新闻标题:{$newsArray[loop].newsTitle}<br><hr>
{sectionelse}
对不起,没有任何新闻输入!
{/section}

希望本文所述对大家基于smarty模板的php程序设计有所帮助。

(0)

相关推荐

  • Smarty高级应用之缓存操作技巧分析

    本文实例讲述了Smarty高级应用之缓存操作技巧.分享给大家供大家参考,具体如下: smarty缓存控制 smarty提供了强大的缓存功能.但有时我们并不希望整篇文档都被缓存,而是有选择的缓存某一部分内容或某一部分内容不被缓存.例如你在页面上端使用一个带有广告条位置的模板,广告条可以包含任何HTML.图象.FLASH等混合信息. 因此这里不能使用一个静态的链接,同时我们也不希望该广告条被缓存. 这就需要在 insert 函数指定,同时需要一个函数取广告条的内容信息.smarty也提供了这种缓存控

  • php smarty模版引擎中的缓存应用

    1,Smarty缓存的配置: $smarty->cache-dir="目录名"; //创建缓存目录名 $smarty->caching=true; //开启缓存,为false的时候缓存无效 $smarty->cache_lifetime=60; //缓存时间,单位是秒 2,Smarty缓存的使用与清除 $marty->display("cache.tpl",cache_id); //创建带ID的缓存 $marty->clear_all_

  • FCKeditor smarty 编辑器的应用PHP

    感谢csdn社区 hsboy用户的帖子 原文: 6 楼hsboy(PHP it!)回复于 2006-03-05 18:44:07 得分60 假设 1.你要用fckeditor编辑的内容通过$smarty->assign('content', $content)传递到模板 2.fckeditor编辑器放在当前被调用的php程序所在目录(注意不是模板文件所在的目录)的fckeditor目录下 则模板只需这样写即可: 复制代码 代码如下: <script type="text/javasc

  • Smarty模板快速入门

    在PHP的世界里已经出现了各式各样的模板类,但就功能和速度来说Smarty还是一直处于领先地位,因为Smarty的功能相对强大,所以使用起来比其他一些模板类稍显复杂了一点.现在就用30分钟让您快速入门. 一. 安装 首先打开网页http://smarty.php.net/download.php,下载最新版本的Smarty.解压下载的文件(目录结构还蛮复杂的).接下来我演示给大家一个安装实例,看过应该会举一反三的.     (1) 我在根目录下建立了新的目录learn/,再在learn/里建立一

  • smarty半小时快速上手入门教程

    本文讲述了smarty快速上手入门的方法,可以让读者在半小时内快速掌握smarty的用法.分享给大家供大家参考.具体实现方法如下: 一.smarty的程序设计部分: 在smarty的模板设计部分我简单的把smarty在模板中的一些常用设置做了简单的介绍,这一节主要来介绍一下如何在smarty中开始我们程序设计.下载Smarty文件放到你们站点中. index.php代码如下: 复制代码 代码如下: <?php /** * * @version $Id: index.php * @package

  • Smarty模板常见的简单应用分析

    本文实例分析了Smarty模板常见的简单应用.分享给大家供大家参考,具体如下: 首先要将smarty这个类包含进来: include_once '../libs/Smarty.class.php'; 然后创一个Smarty对象: $smarty = new Smarty; 可以自定义Smarty 的开始结束符,默认为{ } $smarty->left_delimiter ='<<'; //左符号为 << $smarty->right_delimiter='>>

  • ThinkPHP使用smarty模板引擎的方法

    ThinkPHP支持多种php模板引擎,可以根据个人需要加以配置. 下面我们以Smarty模板引擎为例,给大家说说具体的操作流程! 首先去Smarty官网上下载一个Smarty.本站下载地址:http://www.jb51.net/codes/16086.html. 接下来解压压缩包,会有两个文件夹:demo和libs.打开libs文件夹,复制所有内容. 接下来,打开你网站根目录 下thinkphp的文件夹.里面有个vendor文件夹,这个文件夹是TP调用第三方类库用的,把刚才复制的东西全部粘贴

  • Smarty安装配置方法

    下载最新的Smarty:http://smarty.php.net/ 当前最新版本:2.6.18 解压后将目录中的libs目录重命名为smarty,复制到你的网站目录,同时在网站根目录下建立templates和templates_c两个目录 建立test.php,内容如下: <?php include_once('./Smarty/Smarty.class.php');$smarty = new Smarty(); $smarty -> template_dir = "./templ

  • PHP文件缓存smarty模板应用实例分析

    本文实例分析了PHP文件缓存smarty模板应用.分享给大家供大家参考,具体如下: 一.使用缓存 要开启smarty的缓存,只需将caching设为true,并指定cache_dir即可. 使用cache_lefetime指定缓存生存时间,单位为秒 要对相同页面生成多个不同的缓存,在display或fetch中加入第二参数cache_id,如: $smarty->display('index.tpl',$my_cache_id); 此特性可用于对不同的$_GET进行不同的缓存   二.清除缓存

  • smarty简单应用实例

    本文讲述了smarty简单应用实例.分享给大家供大家参考,具体如下: <?php require 'smarty/libs/Smarty.class.php'; $smarty = new Smarty; $smarty->template_dir="smarty/templates/templates"; $smarty->compile_dir="smarty/templates/templates_c"; $smarty->config

  • smarty简单入门实例

    本文以一个简单实例分析了smarty的用法.分享给大家供大家参考.具体分析如下: smarty建立配置文件.显示文件后即可,配置文件显示文件分开来,使美工跟开发工作能完美分开   配置页面代码:index.php 复制代码 代码如下: <?php  @header("Content-type: text/html; charset=UTF-8");  require '../libs/Smarty.class.php';  $my=new Smarty;  $my->tem

  • Smarty简单生成表单元素的方法示例

    本文实例讲述了Smarty简单生成表单元素的方法.分享给大家供大家参考,具体如下: smarty生成表单元素功能实现的原理是:给smarty一个数组,用于生成和显示菜单或选项,另外在传递一个选项的值,用于默认选择的匹配: 示例如下: php文件:index.php <?php include("smarty_inc.php"); $smarty->assign('cust_ids',array(1000,1001,1002,1003)); $smarty->assig

  • smarty简单分页的实现方法

    本文实例讲述了smarty简单分页的实现方法,分享给大家供大家参考.具体实现方法如下: 以下是模板中的smarty代码,用smarty简单的代入相关的变量就行了,非常简单,但是在php代码中还要传入page这个参数.我觉得这样分挺好,非常简单.我越来越喜欢用smarty了.   Php代码如下: 复制代码 代码如下: {if $pageCount > 1}  {foreach  item=i from=$pagerList}    {if $pageNum eq $i}    {$i}     

  • vue中的非父子间的通讯问题简单的实例代码

    官网上的例子好晦涩,看了一个头两个大,关于非父子间的通讯问题,经过查阅得到了下面的例子, <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>兄弟之间的通讯问题</title> <script src="vue.js"></script> </head>

  • AJAX简单应用实例-弹出层

    function createobj() {  if (window.ActiveXObject) {          return(new ActiveXObject("Microsoft.XMLHTTP"));      }      else if (window.XMLHttpRequest) {          return(new XMLHttpRequest());      } } function personalInfo() {   var oBao=creat

  • bootstrap导航栏、下拉菜单、表单的简单应用实例解析

    制作效果图如下: 代码如下(以后做东西可以改改就能直接用): <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport"

  • spring和quartz整合,并简单调用(实例讲解)

    工作中会定时任务~简单学习一下. 第0步: 工欲善其事必先利其器,首先要做的自然是导包了. 在spring配置包扫描以及在 pom导入包 spring.xml: pom.xml 1.在spring-quartz.xml(和spring.xml同一个位置)配置相关属性 xml的头部每个人都可能不一样,这个自己要用的时候注意. quartz表达式根据自己需求去写,不列举了,这里的是1秒一次的. 2.Task包下配置类 我们这边将定时任务存放到一个包中,命名为task.用spring的自动注解serv

  • asp.net反射简单应用实例

    本文实例讲述了asp.net反射简单应用.分享给大家供大家参考,具体如下: 反射提供了封装程序集.模块和类型的对象(Type 类型).可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性.如果代码中使用了属性,可以利用反射对它们进行访问.----这是反射最简单的理解.下面就是一个最简单的实例来讲述反射技术的应用! 一. 声明接口,接口中包含一个虚方法.如下 using System; using System.Collections.Gener

  • JavaScript正则表达式简单实用实例

     1.replace var user='xia&&min**'; user=user.replace(/[^A-Za-z\d_-]+/,''); //需要再次赋值 console.log(user); //xiamin** user = user.replace(/[^A-Za-z\d_-]+/g, ''); //全局进行替换 console.log(user); //xiamin 分割邮件 var email='nettuts@tutsplus.com'; var result=ema

随机推荐