在Smarty模板中主要都是以变量为主,下面就是有关Smarty的三大变量以及一些普通的用法
首先在根目录下建立要用到的文件基本和上一次一样,configs是配置文件夹
一、从PHP中分配的变量
用$smarty->assign()
$smarty->display()
首先先编写初始化的php代码,和上一篇的 conn.inc.php 一样
<?php
include "init.inc.php";
//assign方法
$smarty->assign("title","11111111");
$smarty->assign("content","2222222222");
//分配一个关联数组,用的较少
$smarty->assign(array("author"=>"何栘烽","time"=>date("Y-m-d H:i:s")));
//注册函数
$smarty->registerPlugin("function","myfun","test"); //注册插件,将test函数注册成myfun
function test($args){
//args=array("size"=>7, "color"="yellow", "num"=>5, "connect"=>"3333333")
//循环遍历出
$str="";
for($i=0; $i<$args["num"]; $i++){
$str.='<font color="'.$args['color'].'" size="'.$args['size'].'">'.$args["content"].'</font><br>';
}
return $str;
}
//数组
$smarty->assign("contacts",array("0575-1241243","kefeng.hyf@gmail.com", array("243214124", "45345")));
//对象
class Person{
public $name="kefeng";
function say(){
return $this->name."hhhhhhhh";
}
}
$smarty->assign("p",new Person());
$smarty->display("demo.html"); //显示的模板
demo.html:
<html>
<head>
<title><{ $title }></title>
<style>
body {color:red;}
</style>
</head>
<body>
content:<{$content}><br>
content:<{$content}><br>
content:<{$content}><br>
author:<{$author}><br>
time:<{$time}><br>
time:<{myfun()}><br>
time:<{date("Y-m-d")}><br>
<img src="img.gif" /><br>
<{myfun size="7" color="yellow" num="5" content="3333333"}>
<{$contacts[0]}><br>
<{$contacts[2][0]}><br>
<br>
<{$p->name}><br>
<{$p->say()}><br>
<br>
</script>
</body>
</html>
二、从配置文件中读取变量
这里需要创一个 configs 文件夹,里面配置文件 test.conf
test.conf:
bodycolor=#3e3e3e
bodybgcolor=yellow
border=10
align=center
width=800
bgcolor=gray
[index]
one=11111
[list]
two=22222
[content]
three=33333
在demo.php 中开启session
<?php
//session_start();
include "init.inc.php";
$_SESSION["username"]="admin";
$smarty->display("demo.html"); //显示的模板
注意的是:
加载配置文件:<{config_load "test.conf"}>
读取:<{#内容#}>
加载区域文件要用到第二个参数:<{config_load "test.conf" section="index"}>
demo.html :中,这里写一些头文件,以及加载代码
<html>
<head>
<title><{ $title }></title>
<style>
body {color:red;}
</style>
</head>
<{config_load "test.conf" section="index"}>
<body text="<{ #bodycolor# }>" bgcolor="<{#bodycolor#}>">
.......
.......
<{#one#}><br>
<{#two#}><br>
<{#three#}><br>
......
</body>
</html><span style="white-space:pre"> </span>
三、保留变量
主要有: $_GET
$_POST
$_SESSION
$_SERVER
$_ENV
<{$smarty()}>
<p>
<{$smarty.session.username}><br>
<{$smarty.now}><br>
<{$smarty.const.ROOT}><br>
<{$smarty.const.M_PI}><br>
<{$smarty.current_dir}><br>
版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
主要介绍了Smarty模板语法,结合实例形式详细分析了Smarty模板的注释、变量、引号、函数等基本语法与使用技巧,需要的朋友可以参考下
php之smarty入门
计算机前端-核心编程. Smarty14Smarty常用的变量调节器.avi
菜鸟学PHP之Smarty入门 php入门级教程 手把手教你学会smarty
(跟我PHP中常用的PHP内部函数类似)2、如何使用Smarty变量操作符语法中使用"|"应用变量操作符,多个参数用":" 分隔开来3、介绍常用的20个变量符capitalize [首字母大写] count_characters [计算字符数] cat [连接...
菜鸟学PHP之Smarty入门菜鸟学PHP之Smarty入门
php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)
php模板引擎smarty内置的一些操作函数,我们称之为变量操作符,变量操作符 可用于操作变量,自定义函数和字符。(跟我PHP中常用的PHP内部函数类似) 2、如何使用Smarty变量操作符 语法中使用"|"应用变量操作符,多...
php Smarty
1. PHP的基本语法。 2. SMARTY的使用。 3. 环境的搭建。 4. 第一个示例。 http://blog.csdn.net/dengxingbo/archive/2009/08/16/4450808.aspx
1、什么是Smarty变量操作符? 2、如何使用Smarty变量操作符 3、介绍常用的20个变量符
3. Basic Syntax [基本语法] Comments [注释] Functions [函数] Attributes [属性] Embedding Vars in Double Quotes [双引号里值的嵌入] Math [数学运算] 4. Variables [变量] Variables assigned from PHP ...
Smarty是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。简单的讲,目的就是...
主要介绍了PHP模板引擎Smarty中的保留变量用法,较为详细的分析说明了Smarty中的保留变量的功能与具体使用方法,需要的朋友可以参考下
php+smarty输出资讯列表,生成html静态
计算机前端-核心编程. Smarty05Smarty中的普通变量.avi
Smarty是一个使用PHP写出来的模板引擎,是业界最著名的PHP模板引擎之一。Smarty分离了逻辑代码和外在的内容,提供一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。Smarty工作的目的是要...
菜鸟学PHP之Smarty入门(组图)
主要介绍了smarty模板引擎中变量及变量修饰器用法,实例分析了smarty模板引擎中常见变量、变量修饰器及常量的具体用法,需要的朋友可以参考下
php+smarty分页类的具体使用之一