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. beego 自定义模板函数

    beego支持的模板函数不是很多,有时候前端展现数据的时候,要对数据进行格式化,所以要用到自定义模板函数 比如我的前端模板上有时间和模板大小这2个数据,原始数据都是int的时间戳和byte单位的数据, ...

  2. 802.11bgn信道划分及WirelessMon规划频段

    一.802.11bgn模式支持14信道,第14信道一般不使用.对应频率范围如下: Channel Frequency range Central Frequency ----------------- ...

  3. nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c

    首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sys ...

  4. C++——代码运行过程详解

    #include <iostream> using namespace std; ;//初始化的全局变量:保存在数据段 char *p1;//未初始化的全局变量:保存在BSS段 int m ...

  5. ios7 适配

    1.状态栏20px高度问题 ) { [application setStatusBarStyle:UIStatusBarStyleLightContent]; self.window.clipsToB ...

  6. php使用curl 实现GET和POST请求(抓取网页,上传文件),支持跨项目和跨服务器

    一:curl 函数和参数详解 函数库:1:curl_init 初始化一个curl会话2:curl_close 关闭一个curl会话3:curl_setopt 为一个curl设置会话参数4:curl_e ...

  7. QUrl

    QUrl Detailed Description The QUrl class provides a convenient interface for working with URLs. It c ...

  8. PHP中PSR

    PSR 是 PHP Standard Recommendations 的简写,由 PHP FIG 组织制定的 PHP 规范,是 PHP 开发的实践标准. 文档整理 PSR-0: Autoloading ...

  9. [GO]runtime包及gosched的使用

    Gosched:让出CPU时间片 Goexit:退出当前的协程 GOMAXPROCS:设置使用最大的CPU数量(哇,牛逼了...) package main import ( "fmt&qu ...

  10. [GO]panic的应用

    对于异常的处理,error表示的是不太致使的错误,但是如果遇到数组越界或者是空指针这种会导致程序崩溃无法恢复的错误时,就需要使用以panic了 我们不应该使用panic去报error的错误,而是只使用 ...