官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 盐山县| 新丰县| 塔河县| 大石桥市| 祁阳县| 宜宾县| 克山县| 沛县| 固安县| 新密市| 大同县| 哈巴河县| 镇原县| 太仆寺旗| 班戈县| 洪洞县| 肇庆市| 隆化县| 师宗县| 游戏| 夏邑县| 兴城市| 元氏县| 安泽县| 潞城市| 广灵县| 苏尼特左旗| 孟村| 疏附县| 松桃| 临潭县| 尖扎县| 荣昌县| 宁晋县| 碌曲县| 安远县| 苍南县| 丰台区| 湘潭市| 霍邱县| 枣强县|