年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2133|回复: 0

模板引擎Smarty从配置文件中读取变量

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2016-1-27 15:44:28 | 显示全部楼层 |阅读模式
    1、设置配置文件路径
    [PHP] 纯文本查看 复制代码
    <?php
    	define(ROOT, dirname(__FILE__));
    	include ROOT.'/libs/Smarty.class.php';
    	$smarty = new Smarty;
    	// 设置模板路径,默认路径为当前php文件所在目录下的templates目录
    	$smarty->setTemplateDir(ROOT."/templates");
    	// 添加模板路径
    // 	$smarty->addTemplateDir("./Home");
    	// 设置编译文件路径,默认路径为当前php文件所在目录下的templates_c目录
    	$smarty->setCompileDir(ROOT."/templates_c");
    	// 设置定界符
    // 	$smarty->setLeftDelimiter("{");
    	// 设置配置文件目录
    	$smarty->setConfigDir(ROOT."/config");
    2、创建配置文件,文件名可自定义,如php.conf;
    3、设置配置文件内容
    [HTML] 纯文本查看 复制代码
    border = 1
    width = 400
    bgcolor = red
    color = blue
    bg = pink
    
    [index]
    a = 11111
    b = 22222
    
    [list]
    c = 33333
    
    [admin]
    d = 44444

    4、模板调用,使用{config_load file="php.conf" section=index},如果不加sectiion,默认只读取全局变量
    [HTML] 纯文本查看 复制代码
    Home.index.html----{$content}<br/>
    {config_load file="php.conf" section=index}
    <body bgcolor="{#bg#}" text="{#color#}">
    	<table border="{#border#}" width="{#width#}">
    		<tr bgcolor="{#bgcolor#}">
    			<td>{#a#}</td>
    			<td>{#b#}</td>
    			<td>{#c#}</td>
    			<td>{#d#}</td>
    		</tr>
    		<tr bgcolor="{#bgcolor#}">
    			<td>ddd</td>
    			<td>ddd</td>
    			<td>ddd</td>
    			<td>ddd</td>
    		</tr>
    		<tr bgcolor="{#bgcolor#}">
    			<td>ddd</td>
    			<td>ddd</td>
    			<td>ddd</td>
    			<td>ddd</td>
    		</tr>
    	</table>
    </body>

    5、效果如图所示:
    屏幕快照 2016-01-27 下午3.43.57.png
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-3-29 22:43 , Processed in 0.061545 second(s), 25 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表