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. 2018-2019第一学期Java助教心得

    随着期末考试落下了帷幕,本学习也结束了回顾本学期的历程,对我影响最深的还是这学期很幸运的成为代老师的助教,这也是我第一次接触助教工作.刚开始的时候我心里也有很多的担心,怕自己胜任不了这份工作,但随着时 ...

  2. Centos7.2下编译安装python3.7

    1.安装python3.7所需要的依赖. yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel rea ...

  3. Python Beautiful Soup 解析库的使用

    Beautiful Soup 借助网页的结构和属性等特性来解析网页,这样就可以省去复杂的正则表达式的编写. Beautiful Soup是Python的一个HTML或XML的解析库. 1.解析器 解析 ...

  4. 10-最小生成树-Prim算法

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; #def ...

  5. SuperWebSocket

    SuperWebSocket: 概述:SuperWebSocket是WebSocket服务器的.NET实现. 简介:WebSocket是通过单个传输控制协议(TCP)插座提供双向,全双工通信信道的技术 ...

  6. java 线程的几个注解

    Java并发编程中,用到了一些专门为并发编程准备的 Annotation. 主要包括三类: 类 Annotation(注解) 就像名字一样,这些注解是针对类的.主有要以下三个: @ThreadSafe ...

  7. nltk 之 snowball 提取词干-乾颐堂

    机器学习中很重要的应用场景就是机器自动分类,而分类的关键是词干提取.所以我们要用到snowball.下面说一下snowball 提取词干的两种方法. 两种方法: 方法一: >>> f ...

  8. [Email] 收发邮件的协议 : IMAP and SMTP , POP3 and SMTP

    支持 IMAP 和 SMTP 的应用 与仅同步收件箱的 POP 不同,IMAP 同步所有电子邮件文件夹. 在电子邮件应用中使用以下设置. 接收 (IMAP) 服务器 服务器地址:imap-mail.o ...

  9. 两个线程并发执行以下代码,假设a是全局变量,那么以下输出______是不可能的?

    3.两个线程并发执行以下代码,假设a是全局变量,那么以下输出______是不可能的? void foo(){    ++a;    printf("%d ",a);}A.3 2   ...

  10. css确定取消 悬浮底部样式 和 金额 后缀

    .blockquote-bottom { width: 100%; position: fixed; margin: 0; bottom: 0; left: 0; text-align: center ...