smarty获取变量的两种方式

从上一篇随笔中,我们知道smarty可以通过assign()的方法注册变量,从而在前段读取变量;我们也可以从配置文件中获取变量,来具体看一下:

1、在configs文件夹中建一个test.conf文件:

在配置文件中设置颜色:

1
color=green

2、在test.html中引入配置文件

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
 
<body>
     
<!--引入配置文件;并改名字;加上尖括号--> 
<{config_load file= 'test.conf' }>
     <!--用##引入配置-->
<div style= "height: 200px;width: 300px;background-color:<{#color#}>;" ></div> 
 
 
</body>
</html>

  3、显示效果

那么,当配置文件中有两个color时,怎么办呢?

1、配置文件:test.conf    (分模块区分)

1
2
3
4
5
[one]
color=yellow
 
[two]
color=red

 2、test.html如何引用?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
 
<body>
     
<!--引入配置文件;并改名字;加上尖括号;section= "one" :取哪个模块用哪个-->  
<{config_load file= 'test.conf' section= "one" }>
     <!--用双井号引入配置-->
<div style= "height: 200px;width: 300px;background-color:<{#color#}>;" ></div> 
 
 
</body>
</html>

  3、效果显示为黄色

 

转载于:https://www.cnblogs.com/Liangbingbing/p/7153835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值