4.模板的繼承及Bootstrap實現導航條

在建設一個網站的時候,不一樣的頁面有不少元素是同樣的,好比導航條、側邊欄等,咱們可使用模板的繼承,避免重複編寫html代碼。如今咱們打算實現一個在網頁上方的導航條,並在全部的頁面中繼承這個導航條。導航條的創建,咱們直接使用Bootstrap提供的以下導航條的樣式css

clipboard.png

但在使用Bootstrap的導航條樣式以前,須要先引用Bootstrap所須要的cssjs文件以及jQuery,咱們在htmlheader中插入如下代碼完成引用:html

<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous">

這裏都是經過連接引用網絡上的cssjs文件,而不是將其下載下來並從本地引用。以後咱們把上述導航條的代碼複製到htmlbody中,jscss引用複製到header中,瀏覽器就能顯示和圖中同樣的導航條了。咱們再作一些簡單的修改和優化,最終咱們的導航條是這樣的:python

clipboard.png

具體修改的點是,我把原始的Brand替換成了一個圖片做爲logo,第一個下拉控件Dropdown刪掉了,最右側的下拉控件增長了一個選項,並把文字都替換成了咱們想要的。而後創建了一個base.css文件來調整圖片大小、控件位置、背景色等等,這一部分都是基礎的html/css知識,也就很少說。後續全部的網頁都要使用這個導航條,咱們將含有導航條這個html命名爲base.html,並在其body中,導航條代碼的下方增長如下代碼:jquery

{% block body_part %}
{% endblock %}

而後新建一個home.html,輸入如下內容:flask

{% extends 'base.html' %}
{% block body_part %}
<p>This is body content under nav-bar</p>
{% endblock %}

渲染home.html並訪問,咱們能夠看到這樣的結果:bootstrap

clipboard.png

所以咱們不難理解,在home.html中,{% extends 'base.html' %}表示繼承自base.htmlhome.htmlblockendblock區間的代碼塊會自動替換到base.html一樣名爲body_partblock區域。block可使用多個,例如在<title>中也可使用,以便於不一樣的頁面設置不一樣的標題。
最終base.html代碼以下:瀏覽器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="{{ url_for('static',filename='css/base.css') }}"/>
    <link rel="shortcut icon" href="{{ url_for('static', filename='images/favicon.ico') }}">
    <title>{% block page_name %}{% endblock %}-HarpQA</title>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand">
              <img class="logo" src="{{ url_for('static',filename='images/logo.png') }}">
          </a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">首頁 <span class="sr-only">(current)</span></a></li>
            <li><a href="#">發佈問答</a></li>
          </ul>
          <form class="navbar-form navbar-left">
            <div class="form-group">
              <input type="text" class="form-control" placeholder="Key Words">
            </div>
            <button type="submit" class="btn btn-default">搜索</button>
          </form>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">登陸</a></li>
            <li><a href="#">註冊</a></li>
             <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">友情連接<span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="mailto:liutao25@baidu.com">聯繫我</a></li>
                <li><a href="http://flask.pocoo.org" target="_blank">Flask官網</a></li>
                <li><a href="https://www.python.org/">Python官網</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="https://www.baidu.com" target="_blank">百度搜索</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="https://www.google.com.hk" target="_blank">Google Search</a></li>
              </ul>
            </li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
</nav>
{% block body_part %}
{% endblock %}
</body>
</html>

請注意一下base.csslogo圖片的引用,咱們也使用了url_for函數,第一個參數是static,表明項目文件夾下static文件夾,第二個參數是以static文件夾爲基準靜態文件的相對路徑,咱們把js文件/css文件/圖片文件等都放在這個文件夾下,因此這個用法之後會常常使用。咱們在收藏網頁的時候,網頁都有一個小圖標,咱們也能夠在header中使用這行html代碼來實現:網絡

<link rel="shortcut icon" href="{{ url_for('static', filename='images/favicon.ico') }}">

favicon.ico文件放在static/images文件夾便可,咱們使用了Flask的圖標,效果以下圖:ide

clipboard.png

base.css代碼以下:函數

body{
    background: #F3F3F3;
}

.navbar-brand{
    padding: 0 5px;
    padding-right: 10px;
}

.logo{
    height: 50px;
}
相關文章
相關標籤/搜索