- 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.
- 少兒人工智能趣味入門(mén):Scratch 3.0動(dòng)畫(huà)與游戲編程
- Google Apps Script for Beginners
- Hyper-V 2016 Best Practices
- 深入淺出Electron:原理、工程與實(shí)踐
- 算法精粹:經(jīng)典計(jì)算機(jī)科學(xué)問(wèn)題的Python實(shí)現(xiàn)
- 老“碼”識(shí)途
- Web Application Development with MEAN
- Hands-On Functional Programming with TypeScript
- C++從入門(mén)到精通(第5版)
- 大話(huà)Java:程序設(shè)計(jì)從入門(mén)到精通
- JavaScript程序設(shè)計(jì)(第2版)
- Learning Nessus for Penetration Testing
- Java自然語(yǔ)言處理(原書(shū)第2版)
- Less Web Development Cookbook
- 透視C#核心技術(shù):系統(tǒng)架構(gòu)及移動(dòng)端開(kāi)發(fā)