html模板继承

模板继承

基本模板

可以定义一个简单的html骨架文档,用内容填充的块是子模版的工作。
其中{%block%}标签定义的是可以填充内容的地方,告诉模板引擎子模板可以覆盖模板中的这些部分。

<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    {% block head %}
    <link rel="stylesheet" href="style.css" />
    <title>{% block title %}{% endblock %} - My Webpage</title>
    {% endblock %}
</head>
<body>
    <div id="content">{% block content %}{% endblock %}</div>
    <div id="footer">
        {% block footer %}
        &copy; Copyright 2008 by <a href="http://domain.invalid/" rel="external nofollow" target="_blank" >you</a>.
        {% endblock %}
    </div>
</body>

添加链接描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Laravel是一款流行的PHP框架,也支持模板继承。Laravel使用了Blade模板引擎来实现模板继承。在Laravel中,模板继承的基本结构包括三个文件: 1. 基础模板(layout.blade.php):定义整个网站的布局和结构,包括头部、导航、侧边栏、底部等。基础模板中使用`@yield`指令来定义占位符,这些占位符可以在子模板中被替换成具体的内容。 2. 子模板(child.blade.php):继承基础模板并添加自己的内容。子模板中使用`@extends`指令来声明继承关系,并使用`@section`指令来定义具体内容。 3. 具体内容(content.blade.php):包含了子模板所需的具体内容。具体内容必须通过`@section`指令来定义,并且必须与基础模板中的占位符对应。 一个简单的例子如下: 基础模板(layout.blade.php): ```html <html> <head> <title>@yield('title')</title> </head> <body> <div class="container"> @yield('content') </div> </body> </html> ``` 子模板(child.blade.php): ```html @extends('layout') @section('title', 'Page Title') @section('content') <p>This is my page content.</p> @endsection ``` 具体内容(content.blade.php): ```html @extends('child') @section('content') <p>This is my updated page content.</p> @endsection ``` 在这个例子中,具体内容(content.blade.php)继承了子模板(child.blade.php),子模板继承了基础模板(layout.blade.php)。具体内容中重新定义了`@section('content')`指令,使得其覆盖了子模板中的定义。最终渲染的页面将会是: ```html <html> <head> <title>Page Title</title> </head> <body> <div class="container"> <p>This is my updated page content.</p> </div> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值