- D Web Development
- Kai Nacke
- 227字
- 2021-07-16 09:35:32
Benefits of using templates
Let's look at a simple HTML 5 page with a header, footer, navigation bar, and some content:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Demo site</title> <link rel="stylesheet" type="text/css" href="demo.css" /> </head> <body> <header> Header </header> <nav> <ul> <li><a href="link1">Link 1</a></li> <li><a href="link2">Link 2</a></li> <li><a href="link3">Link 3</a></li> </ul> </nav> <article> <h1>Title</h1> <p>Some content here.</p> </article> <footer> Footer </footer> </body> </html>
The formatting is done with the demo.css
file:
body { font-size: 1em; color: black; background-color: white; font-family: Arial; } header { display: block; font-size: 200%; font-weight: bolder; text-align: center; } footer { clear: both; display: block; text-align: center; } nav { display: block; float: left; width: 25%; } article { display: block; float: left; }
Despite being simple, this page has elements that you will often find on other websites. If you create a website with more than one page, then you will be using this structure on every page in order to provide a consistent user interface. Starting with the second page, you would violate the Don't Repeat Yourself (DRY) principle: The header and footer are the elements with fixed content. The content of the navigation bar is also fixed but not every item is always displayed. Only the real content of the page (in the article
block) changes with every page.
Templates solve this problem. A common approach while using templates is to define a base template with the page structure. For each page, you will define a template that inherits from the base template and adds the real content.
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- 嵌入式軟件系統測試:基于形式化方法的自動化測試解決方案
- 無代碼編程:用云表搭建企業數字化管理平臺
- PHP 7底層設計與源碼實現
- 動手玩轉Scratch3.0編程:人工智能科創教育指南
- Swift 3 New Features
- Learning Apache Kafka(Second Edition)
- Big Data Analytics
- Gradle for Android
- Apache Mahout Clustering Designs
- 微服務架構深度解析:原理、實踐與進階
- C#程序設計教程(第3版)
- 零基礎C#學習筆記
- Mastering Embedded Linux Programming
- WCF技術剖析(卷1)