thinkphp6 入门(2)--视图、渲染html页面、赋值

修改模板引擎

config/view.php

    // 模板引擎类型使用Think    'type' => 'php',

2. 新建一个控制器

本文app的名称为test,在其下新建一个控制器User

app/test/controller/User.php

注意:需要引用think\facade\View来操作视图

<?phpnamespace app\test\controller;
use app\BaseController;// 添加引用use think\facade\View;
class User extends BaseController{    // 渲染页面    public function index(){        // 变量赋值        View::assign('name', '张三');        // 模板输出        return View::fetch('User/index');    }}

使用assign方法进行全局模板变量赋值

return View::fetch('User/index');

表示调用User控制器下面的index模板。

3. 新建一个页面

app/test/view/User/index.html

​​​​​​​

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>用户详情</title></head><body>    <h1>用户详情</h1>    <p>我是<?php echo $name ?></p></body></html>

4. 浏览器访问

访问地址 http://localhost/clubs/public/index.php/test/user/index

5. 若有外部样式文件,则放到public/static文件夹

在html中通过下列的方式进行引用

6. 运算符和标签

运算符、循环标签、if标签、比较标签、条件标签等

请看:https://blog.csdn.net/qzmlyshao/article/details/131013777

7. 官方教程

请看https://www.kancloud.cn/manual/thinkphp6_0/1037608

相关推荐
空暝1 个月前
ThinkPHP的SQL注入漏洞学习
数据库·sql·学习·php·web·thinkphp
板栗妖怪1 个月前
thinkphp5之sql注入漏洞-builder处漏洞
学习·php·渗透·thinkphp·sql注入
逍遥蓝枫叶2 个月前
ThinkPHP6支持金仓数据库(Kingbase)解决无法使用模型查询问题
thinkphp·kingbase
frandiy2 个月前
【黑科技】:Laravel 项目性能提升 20 倍
php·laravel·thinkphp
疯子丶pony2 个月前
ThinkPHP一对一关联模型的运用(ORM)
php·thinkphp
张小勇2 个月前
thinkphp5多层with关联查询错误问题
thinkphp·fastadmin
张小勇3 个月前
thinkphp通过with查询,并通过关联表进行筛选
thinkphp·fastadmin
一一程序3 个月前
ThinkPHP-导入Excel表格(通用版)
php·excel·thinkphp·thinkphp导入excel
张小勇3 个月前
thinkphp单独为某个接口设置缓存
thinkphp
洪、3 个月前
记录一下PHP使用微信小程序支付
微信小程序·php·thinkphp