Correction status:Uncorrected
Teacher's comments:
<?php namespace app\index\controller; use think\Controller; class Index extends Controller { /** * 赋值 * * @return void */ public function setValue() { $data = [ 'title' => '模板赋值', 'content' => '使用tp模板进行赋值', ]; $this->view->author = 'gakkispy'; $this->view->assign($data); return $this->fetch(); } /** * 替换过滤 */ public function filterValue () { $data = [ 'title' => '模板赋值', 'content' => '使用tp模板进行赋值', ]; $this->view->author = 'gakkispy'; $this->view->assign($data); $filter = function($content) { return str_replace('gakkispy', 'moron', $content); }; return $this->filter($filter)->fetch('index@index/set_value'); } /** * 布局 */ public function layout() { return $this->view->engine->layout('layout', '{__MAIN__}')->fetch('index@index/layout_main'); } /** * 继承 */ public function inherit() { return $this->fetch('index/inherit_main'); } }
点击 "运行实例" 按钮查看在线实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>赋值</title> <style> footer { position:absolute; bottom:0; } </style> </head> <body> <h1>{$title}</h1> <div>{$content}</div> <footer>{$author}</footer> </body> </html>
点击 "运行实例" 按钮查看在线实例
{include file="public/header" /} {__MAIN__} {include file="public/footer" /}
点击 "运行实例" 按钮查看在线实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>模板</title> </head> <body> {block name="header"} {include file="public/header" /} {/block} {block name="main"}main{/block} {block name="author"}author{/block}</br> {block name="footer"} {include file="public/footer" /} {/block} </body> </html>
点击 "运行实例" 按钮查看在线实例
{extend name="base" /} {block name="main"} <div class="main">模板继承练习</div> {/block} {block name="author"} <div class="main">moron</div> {/block}
点击 "运行实例" 按钮查看在线实例