PHP Smarty 模板引擎,phpsmarty模板引擎
模板引擎就是将php和html分离开来。将显示和逻辑分离开来。
Smarty是一个PHP的模板引擎,提供让程序逻辑与页面显示(HTML/CSS)代码分离的功能。 也就是PHP代码是程序逻辑,与页面显示分开。(Mvc是站在整个项目的角度;模板是站在视图的角度。)
Smarty下载: https://www.smarty.net/download
编写PHP代码文件(后台php程序员)(Smarty目录结构):
编写模板文件(前端开发人员):
index.php(后端):
<?php //1.引入smarty类 include 'libs/Smarty.class.php'; //2.实例化smarty对象 $smarty = new Smarty(); //3.设置相关属性 $smarty->template_dir = "templates/"; //模板目录 $smarty->compile_dir = "templates_c"; //编译目录 //4.分配数据 $smarty->assign('title','smarty模板引擎'); $smarty->assign('content','smarty模板引擎 是一个强大的模板引擎!'); //5.载入视图 $smarty->display('index.html'); //后缀.html可以随便写templates/index.html(前端视图):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{$title}</title> </head> <body> <h1>{$title} $title</h1> <p>{$content}</p> <p><?php echo $title;?></p> </body> </html>
PHP之友评论