PHP Smarty 模板引擎

模板引擎就是将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>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值