使用Layui模板引擎需要先在ThinkPHP中引入Layui静态资源文件,具体操作如下:
在Layui官网下载所需的静态资源文件。下载地址:https://www.layui.com/download/
将下载好的Layui静态资源文件解压并复制到ThinkPHP项目的public目录下。
在ThinkPHP项目的控制器中使用assign()方法将数据传递到模板文件中,例如:
public function index()
{
$data = array('name' => 'Layui', 'author' => '贤心');
$this->assign('data', $data);
return $this->fetch();
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>使用Layui模板引擎</title>
<link rel="stylesheet" href="/public/layui/css/layui.css">
</head>
<body>
<div class="layui-container">
<div class="layui-row">
<div class="layui-col-md12">
<h2>{{ data.name }}</h2>
<p>作者:{{ data.author }}</p>
</div>
</div>
</div>
<script src="/public/layui/layui.js"></script>
</body>
</html>
在模板文件中使用双括号{{}}来输出变量的值,注意要使用Layui提供的样式类来美化页面。
需要注意的是,Layui模板引擎的使用需要引入Layui的静态资源文件,因此在使用Layui模板引擎之前,需要先安装Layui静态资源文件。