---
title: "TP5 fetch() 渲染模板输出"
author: "onestopweb"
date: "2017-08-06T11:49:29Z"
tags: []
categories: ["Web前端"]
slug: "tp5-fetch"
draft: false
lastmod: "2017-08-06T11:49:29Z"
id: 2388444
url: "https://onestopweb.iteye.com/blog/2388444"
---

app\\index\\controller\\Index.html

```
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    public function index()
    {
        //模板变量赋值
        $username = 'chaoyi';
        $this -> assign('username',$username);
        /**
         * 渲染模板输出
         * 第一参数：模板文件
         * 第二参数：模板变量（数组）
         * 第三参数：模板替换（数组）
         */
        return $this->fetch('index',[
            'email' => 'onestopweb@163.com',
            'dictum' => '一直专注于网站'
        ],[
            '一直' => '永远'
        ]);
    }
}
```

 

app\\index\\view\\index\\index.html

```
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>一直如示</title>
</head>
<body>
<p>我的名字：{$username}</p>
<p>我的电子邮箱：{$email}</p>
<p>我的口号：{$dictum}</p>
</body>
</html>
```

 

效果图：  
![](http://dl2.iteye.com/upload/attachment/0126/4340/bc6f066e-7ae6-3091-8a45-9e646a78e9ad.png)