smarty模板引擎从配置文件中获取数据的方法,smarty模板
本文实例讲述了smarty模板引擎从配置文件中获取数据的方法。分享给大家供大家参考。具体如下:
当某个变量值,不希望在程序中写死时,就可以把该变量写到配置文件里,并从中获取(常见的配置样式)。
第一步:先写一个配置文件,如数据库的 db.conf,后缀名conf可以随便写,db.ini也行。文件中内容的格式需要固定:key="值",每一行后面不需要添加分号或者什么,直接回国换行,如:
配置文件:db.conf
复制代码 代码如下:
host = "localhost"
username = "root"
password = "123456"
db_name = "liuyan"
模板文件:temp.tpl
使用{config_load file="db.conf"} 将文件引入进来。注意,如果写相对路径的话,要以访问的页面来看。比如这里,temp.tpl放在templates目录下,db.conf是放在与templates目录同一层,但由于浏览器访问的文件index.php与db.conf同一层,所以,引用时,直接写 {config_load file="db.conf"} 。
复制代码 代码如下:
{config_load file="db.conf"}
smarty变量操作,从配置文件中获取
{#host#}
{#username#}
浏览器访问:index.php
与从php获取变量数据不同,这里不需要用assign分配,而在模板文件里直接加载
复制代码 代码如下:
//创建smarty对象
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->display("index.tpl");
?>
希望本文所述对大家的smarty程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use