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)的更多相关文章

  1. 二 ,Smarty模板技术/引擎——变量操作(1)

    1,基本变量 $smarty->assign('data1',3); $smarty->assign('data2',3.45); $smarty->assign('data3',' ...

  2. 一,Smarty模板技术/引擎——简介

    Smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使PHP程序员与美工分离,使用的程序员改变程序的逻辑内容不会影响到美工的页面设计,美工重新修改 ...

  3. 五,Smarty模板技术/引擎——自定义函数机制

    自建函数是smarty提供的函数,不允许修改,只能被调用: 自定义函数是自己编写函数,注册成为smarty的函数,之后可以被调用: 示例:使用smarty自定义函数的机制,编写一个函数myfun1,通 ...

  4. 四,Smarty模板技术/引擎-----内建函数

    内建函数是smarty提供的函数,不允许修改,只能被调用: 自定义函数是自己编写函数,注册成为smarty的函数,之后可以被调用. PHP的自建函数很多,讲解下<foreach>和< ...

  5. Smarty模板技术学习

    模板引擎技术:使得php代码和html代码分离的技术就称为"模板引擎技术" 自定义smarty模板技术实现 <?php //迷你smarty原理 class MiniSmar ...

  6. Smarty模板技术学习(二)

    本文主要包括以下内容 公共文件引入与继承 内容捕捉 变量调剂器 缓存 Smarty过滤器 数据对象.注册对象 与已有项目结合 公共文件引入与继承 可以把许多模板页面都用到的公共页面放到单独文件里边,通 ...

  7. smarty模板技术

    一.什么是smarty?smarty是一个使用php写出来的模板php模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用php程序员同美工分离,使用的程序员改变程序的逻辑内容不会影响到美 ...

  8. smarty详细使用教程(韩顺平smarty模板技术笔记)

    MVC是一种开发模式,强调数据的输入.处理.显示是强制分离的 Smarty使用教程1.如何配置我们的smarty解压后把libs文件夹放在网站第一级目录下,然后创建两个文件夹templates 存放模 ...

  9. Smarty模板技术之foreach遍历数组实例全面讲解

    一.item属性用法 <?php $arr = array(, , ); $smarty->assign('testarrg', $arr); ?> 用Smarty中的foreach ...

随机推荐

  1. Lamber算法 & SurfaceShader自定义光照

    [SurfaceShader自定义光照] 1.在pragma中添加自定义光照函数名:  #pragma surface surf BasicDiffuse 2.实现自定义光照函数.下面就是Lamber ...

  2. OpenCV之设计模式

    参考资料: http://hahack.com/codes/opencv-and-mvc/ http://blog.csdn.net/yzhang6_10/article/details/508084 ...

  3. windows下怎么安装protobuf for python

    首先从google上下载protobuf-3.0.0.zip和protoc-3.0.0-win32.zip,然后把protoc-3.0.0-win32.zip里的protoc.exe放到protobu ...

  4. VS优化编译配置

    在使用VS2010编译C++程序的时候,每次修改工程中的某一个文件,点击“生成-仅用于项目-仅生成**”时,往往都是整个工程都需要重新编译一遍.由于这个工程代码量太大,每次编译完成都需要将近10分钟左 ...

  5. ECS 游戏架构 实现

    转载自:http://blog.csdn.net/i_dovelemon/article/details/27230719 实现 组件-实体-系统 - 博客频道   这篇文章是在我前面文章,理解组件- ...

  6. paho_c_pub 使用方法

    Latest Paho Status (2) 摘自:http://modelbasedtesting.co.uk/ I last wrote about the state of Paho in Oc ...

  7. Java中的Set,List,Map的区别

    1. 对JAVA的集合的理解是想对于数组 数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型) JAVA集合可以存储和操作数目不固定的一组数据. 所有的JAVA集合都位于 ja ...

  8. cookie与session组件

    会话跟跟踪技术 cookie介绍 Djanjo中操作Cookle Session Django中Session相关方法 Django中的Session配置 CBV中加装饰器 session中运用aja ...

  9. 4.3.1 ThreadLoacl简单使用

    我们都知道  SimpleDateFormat 这个类是线程 不安全的,那么我下面的程序执行就会遇到问题 public class ParseDateDemo { private static fin ...

  10. vscode填坑之旅: vscode设置中文,设置中文不成功问题

    刚安装好的vscode界面显示中文,如何设置中文呢? 在locale.json界面设置”locale":"zh-cn"也未能实现界面为中文,在网上找了参考了,以下教程真实 ...