博主信息
博文 28
粉丝 0
评论 0
访问量 20728
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
模板赋值、布局、继承等练习-2018年06月1日
植树青年小江同志的博客
原创
801人浏览过

controller部分

实例

<?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');
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


view/idnex/set_value.html

实例

<!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>

运行实例 »

点击 "运行实例" 按钮查看在线实例

view/layout.html

实例

{include file="public/header" /}
{__MAIN__}
{include file="public/footer" /}

运行实例 »

点击 "运行实例" 按钮查看在线实例

view/index/layout_main.html

实例

<div class="main">模板布局练习</div>

运行实例 »

点击 "运行实例" 按钮查看在线实例

view/base.html

实例

<!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>

运行实例 »

点击 "运行实例" 按钮查看在线实例

view/index/inherit_main.html

实例

{extend name="base" /}
{block name="main"}

<div class="main">模板继承练习</div>

{/block}

{block name="author"}

<div class="main">moron</div>

{/block}

运行实例 »

点击 "运行实例" 按钮查看在线实例


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学