三,Smarty模板技术/引擎——变量操作(2)
1, 变量的分类
① 从PHP中分配的变量,比如a.php跳转到b.php时候,可以在a.php中分配变量,b.tpl中直接调用。a.php中代码,$smarty->assign(‘str’,’hello world’);在b.tpl中代码,{$str}直接打印出hello world。
index.php
<?php
require_once "./libs/Smarty.class.php";
$smarty = new Smarty();
$smarty->assign('str','hello world');
$smarty->display('index.tpl');
index.tpl
<html>
<body>
<h1>显示记录</h1>
{$str}
<br/>
</body>
</html>
② 从配置文件中读取变量
在项目下新建config目录,config目录下面新建配置文件my.ini。
my.ini
title = 'smarty显示记录'
bgcolor = 'pink'
index.php
<?php
require_once "./libs/Smarty.class.php";
$smarty = new Smarty(); $smarty->assign('str','hello world');
$smarty->display('index.tpl');
?>
index.tpl
{config_load file="./config/my.ini"}
<html>
<body bgcolor="{#bgcolor#}">
<h1>显示记录</h1>
{#title#}
<br/>
</body>
</html>
页面打印结果

在建立smarty对象后,可以指定配置文件路径,tpl中直接引用即可。
index.php修改为如下样式:
<?php
require_once "./libs/Smarty.class.php";
$smarty = new Smarty();
$smarty->config_dir = './config';
$smarty->assign('str','hello world');
$smarty->display('index.tpl');
?>
index.tpl样式如下:
{config_load file="my.ini"}
<html>
<body bgcolor="{#bgcolor#}">
<h1>显示记录</h1>
{#title#}
<br/>
</body>
</html>
③ Smarty保留变量,在tpl中直接获取get、post、server、session变量。我们通常是在php中获取这些变量,再通过smarty的assign属性发送给tpl。现在通过smarty的保留变量可以直接tpl中获取这些变量,当然我们不提倡这么做。
login.php跳转到index.php,index.php通过smarty模板显示。
login.php
<a href="index.php?name=hello world& age=20">新页面</a>
a) 旧方式
index.php
<?php
require_once "./libs/Smarty.class.php";
$smarty = new Smarty(); $smarty->assign('name',$_GET['name']);
$smarty->assign('age',$_GET['age']);
$smarty->display('index.tpl');
index.tpl
<html>
<body bgcolor="pink">
<h1>显示记录</h1>
{$name}<br/>
{$age}
<br/>
</body>
</html>
b 新方式
index.php
<?php
require_once "./libs/Smarty.class.php";
$smarty = new Smarty();
$smarty->display('index.tpl');
?>
index.tpl
<html>
<body bgcolor="pink">
<h1>显示记录</h1>
{$smarty.get.name}<br/>
{$smarty.get.age}<br/> </body>
</html>
两者输出结果一样,建议使用旧的方式。
2,在模板文件中对引用的变量可以进行数学运算,但不能使用()改变运算的次序。
Smarty的变量调节器,就是操作变量的函数,在tpl中引用的方式{变量调节器|字符串},比如{$title|capitalize}字符串每个单词首字母大写。我们可以自建函数,操作变量。
三,Smarty模板技术/引擎——变量操作(2)的更多相关文章
- 二 ,Smarty模板技术/引擎——变量操作(1)
1,基本变量 $smarty->assign('data1',3); $smarty->assign('data2',3.45); $smarty->assign('data3',' ...
- 一,Smarty模板技术/引擎——简介
Smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使PHP程序员与美工分离,使用的程序员改变程序的逻辑内容不会影响到美工的页面设计,美工重新修改 ...
- 五,Smarty模板技术/引擎——自定义函数机制
自建函数是smarty提供的函数,不允许修改,只能被调用: 自定义函数是自己编写函数,注册成为smarty的函数,之后可以被调用: 示例:使用smarty自定义函数的机制,编写一个函数myfun1,通 ...
- 四,Smarty模板技术/引擎-----内建函数
内建函数是smarty提供的函数,不允许修改,只能被调用: 自定义函数是自己编写函数,注册成为smarty的函数,之后可以被调用. PHP的自建函数很多,讲解下<foreach>和< ...
- Smarty模板技术学习
模板引擎技术:使得php代码和html代码分离的技术就称为"模板引擎技术" 自定义smarty模板技术实现 <?php //迷你smarty原理 class MiniSmar ...
- Smarty模板技术学习(二)
本文主要包括以下内容 公共文件引入与继承 内容捕捉 变量调剂器 缓存 Smarty过滤器 数据对象.注册对象 与已有项目结合 公共文件引入与继承 可以把许多模板页面都用到的公共页面放到单独文件里边,通 ...
- smarty模板技术
一.什么是smarty?smarty是一个使用php写出来的模板php模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用php程序员同美工分离,使用的程序员改变程序的逻辑内容不会影响到美 ...
- smarty详细使用教程(韩顺平smarty模板技术笔记)
MVC是一种开发模式,强调数据的输入.处理.显示是强制分离的 Smarty使用教程1.如何配置我们的smarty解压后把libs文件夹放在网站第一级目录下,然后创建两个文件夹templates 存放模 ...
- Smarty模板技术之foreach遍历数组实例全面讲解
一.item属性用法 <?php $arr = array(, , ); $smarty->assign('testarrg', $arr); ?> 用Smarty中的foreach ...
随机推荐
- K.O. ----- 配置文件没有提示
---------------siwuxie095 K.O. ----- 配置文件没有提示 1.解决方法一:联网 只要 PC 联网,配置文件中就有提示 2.解决方法二:手动导入约束文件 约束文件:hi ...
- 882. Reachable Nodes In Subdivided Graph
题目链接 https://leetcode.com/contest/weekly-contest-96/problems/reachable-nodes-in-subdivided-graph/ 解题 ...
- spring+springmvc+mybatis+redis实现缓存
先搭建好redis环境 需要的jar如下: jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:330 ...
- java中如何实现两个值互换
public class SwapVariable { public static void main(String[] args) { // 将两个数据进行交换: method2(,); metho ...
- Cocos2dx之touch事件
今天看了下ccocos2dx touch事件部分的源码,从CCTouch.CCTouchHandler和CCTouchDispatcher简单的做了分析和总结,先直接看源码吧! 1.CCTouch c ...
- CENTOS7 YUM安装BOOST1.53(静态版本)
按照之前的博文更新163的源之后,执行: yum install boost-static.i686 yum install boost-devel.i686 yum install boost-do ...
- mybatis :xml文件中传入参数和if标签结合使用时要点
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.Reflecti ...
- java Concurrent包学习笔记(二):CountDownLatch和CyclicBarrier
一.CountDownLatch CountDownLatch一个线程同步的工具,是的一个或者多个线程等待其他线程操作完成之后再执行. CountDownLatch通过一个给定的数值count来进行初 ...
- dapper利用DynamicParameters构建动态参数查询
public static int GetTotalLogin(string username,DateTime start, DateTime end) { using (var _connecti ...
- kcp流模式与消息模式对比
kcp的流模式,和消息模式 流模式: 更高的网络利用率 更大的传输速度 解析数据相对更复杂 消息模式: 更小的网络利用率 更小的传输速度 解析数据相对更简单 消息模式的示意图 http://www.p ...