模板继承小案例

原创 2019-01-18 11:15:24 728
摘要:本章主要学习了模板的引用和继承。通过对本章节的学习,把之前写过的小米商城登录页进行改写。代码如下:<?php require __DIR__.'/config/config.php'; $smarty->display(__DIR__ . '/temp/Login.html');config.php:<?php

本章主要学习了模板的引用和继承。通过对本章节的学习,把之前写过的小米商城登录页进行改写。代码如下:

<?php

require __DIR__.'/config/config.php';

$smarty->display(__DIR__ . '/temp/Login.html');

config.php:

<?php

require __DIR__.'/../vendor/autoload.php';

//创建smarty模板对象
$smarty=new Smarty();

//配置目录
$smarty->setTemplateDir(__DIR__ . '/../temp');    //模板目录
$smarty->setCompileDir(__DIR__.'/../temp_c');      //编译目录
$smarty->setCacheDir(__DIR__.'/../cache');          //缓存目录
$smarty->setConfigDir(__DIR__.'/../config');        //配置目录



//配置定界符:可选
$smarty -> setLeftDelimiter('{');    //变量左定界符
$smarty -> setRightDelimiter('}');  //变量右定界符

//配置缓存:可选
$smarty ->setCaching(false);               //关闭缓存
$smarty->setCacheLifetime(60*60*24);  //缓存有效时间

Login.html:

{extends file="LayoutLogin.html"}

{* 头部内容 *}
{block name="headContent"}
<title>小米商城登录</title>
<meta charset="utf-8">
<link rel="shortcut icon" type="image/x-icon" href="temp/static/images/footlogo.png" />
<link rel="stylesheet" type="text/css" href="temp/static/layui/css/layui.css">
<link rel="stylesheet" type="text/css" href="temp/static/css/login.css">
<link rel="stylesheet" type="text/css" href="temp/static/font-awesome/css/font-awesome.min.css">
<script type="text/javascript" src="temp/static/layui/layui.js"></script>
{/block}


{* 头部 *}
{block name="header"}
<div class="header">
    <a href="./index.html"><img src="temp/static/images/5.png"></a>
</div>
{/block}

{* 中间区域 *}
{block name="content"}


<div class="content">
    <div  class="login_content">
        <div  class="login_form">
            <div class="login_form_main">
                <p style="color: #ff6700;" id="regTabs_0" onclick=" ChangeReg('0','register_',1)">账号登录</p>
                <span>|</span>
                <p id="regTabs_1" onclick=" ChangeReg('1','register_',1)">扫码登录</p>
            </div>
            <div class="clear"></div>
            <div class="login_form_content" id="register_0">
                <form>
                    <input type="text" name="username" placeholder="邮箱/ 手机号码/小米ID">
                    <input type="text" name="password" placeholder="密码">
                    <button>登录</button>
                </form>
                <h6><a href="" style="color: #ff6700;">手机短信登录/注册</a><span><a href="">立即注册</a>&nbsp; | &nbsp;<a href="">忘记密码?</a></span></h6>
                <div class="login_form_pic">
                    <p>其他方式登录</p>
                    <ul>
                        <li class="pic1" style="margin-right: 35px;"><i class="fa fa-qq"></i></li>
                        <li class="pic2" style="margin-right: 35px;"><i class="fa fa-weibo"></i></li>
                        <li class="pic3" style="margin-right: 35px;"><i class="fa fa-twitter-square"></i></li>
                        <li class="pic4"><i class="fa fa-weixin"></i></li>
                    </ul>
                </div>

            </div>

            <div class="login_form_content0" style="display: none;" id="register_1">
                <img src="temp/static/images/下载.png">
                <p>使用<span style="color: #ff6700;">小米商城APP</span>扫一扫</p>
                <p>小米手机可打开「设置」>「小米帐号」扫码登录</p>
            </div>
        </div>
    </div>
</div>

{/block}


{* 底部 *}
{block name="footer"}

<p style="margin-top: 60px;">简体<span>|</span>繁体<span>|</span>English<span>|</span> 常见问题<span>|</span> 隐私政策</p>
<p>小米公司版权所有-京ICP备10046444-<img src="temp/static/images/ghs.png"> 京公网安备11010802020134号-京ICP证110507号</p>

{/block}

{* 页面js *}
{block name="javascript"}
{* 引用父级的js *}
{$smarty.block.parent}
{/block}

LayoutLogin.html:

<!DOCTYPE html>
<html>
<head>
    {block name="headContent"}
    {/block}
</head>
<body>
{block name="header"}

{/block}
{block name="content"}

{/block}
{block name="footer"}

{/block}

{block name="javascript"}
<script type="text/javascript">
    function ChangeReg(divId,divName,Count) {
        for(var i=0;i<=Count;i++){
            document.getElementById(divName+i).style.display='none'
        }
        document.getElementById(divName+divId).style.display='block'
        if(divId==0){
            document.getElementById('regTabs_0').style.color='#ff6a00'
            document.getElementById('regTabs_1').style.color='#757575'
        }
        if(divId==1){
            document.getElementById('regTabs_0').style.color='#757575'
            document.getElementById('regTabs_1').style.color='#ff6a00'
        }

    }
</script>
{/block}



</body>
</html>

login.css:

*{margin: 0px; padding: 0px;}
li{list-style:none;}
a{text-decoration: none;color: #ccc;cursor: pointer;}
.clear{clear: both;}

.header{width: 1226px;height: 100px;margin: 0px auto;line-height:90px;padding-left: 40px; }
.content{width: 100%;height: 580px;background: url(../images/login.png);}
p{width: 1226px;height: 38px;margin:0px auto;line-height: 38px;text-align: center;color: #757575;}
span{font-size: 13px;margin:0px 10px;}
.login_content{width: 1226px;height: 520px;margin:0px auto;}
.login_form{width: 410px;height: 520px;float: right;background: #fff;margin: 30px 50px;}
.login_form_main{width: 330px;margin:25px auto; }
.login_form_main p{font-size: 23px;text-align: center;float: left;color: #757575;width: 150px;height: 50px;line-height: 50px;}
.login_form_main span{font-size: 30px;float: left;height: 50px;line-height: 50px;color: #e0e0e0;}
.login_form_content{margin-top: 25px;}
input{border: none;width: 328px;height: 28px;padding: 11px;margin: 15px auto;border:1px solid #e0e0e0;display: block;}
button{border: none;width: 350px;display: block;height: 50px; margin: 25px auto 0px;background: #ff6700;color: #fff;}
.login_form_content h6{font-weight: 300;width: 350px;margin: 0px auto;height: 40px;line-height: 40px;}
.login_form_content span{float: right;}
.login_form_pic{width: 350px;margin:100px auto 0px;border-top: 1px solid #e0e0e0 ; position: relative;}
.login_form_pic p{width: 100px;height: 30px;line-height: 30px;color: #ccc;background: #fff;position: absolute;top:-15px;left: 125px;}
.login_form_pic ul {width: 233px; margin: 20px  auto 0px;}
.login_form_pic ul li{float: left;height: 32px;width: 32px;line-height: 32px;color: #fff;text-align: center;border-radius: 16px;background:#6D6E6A; }
.login_form_pic ul li i{font-size: 17px;}
.pic1:hover{background: #0288d1;}
.login_form_content0{width: 350px;margin: 0px auto;text-align: center;}
.login_form_content0 img{height: 190px;width: 180px;margin:70px auto 15px;}
.login_form_content0  p{width: 350px;margin:0px auto; line-height: 20px;height: 20px;}

效果如下:

QQ截图20190118111409.jpg

批改老师:查无此人批改时间:2019-01-18 11:54:31
老师总结:作业完成的不错,代码很整洁。你可以改写小米商城登录页,说明你对编程有更深的理解了,继续加油。

发布手记

热门词条