{"id":1738,"date":"2024-08-01T20:16:36","date_gmt":"2024-08-01T14:46:36","guid":{"rendered":"https:\/\/geekpython.in\/?p=1738"},"modified":"2024-08-01T20:16:37","modified_gmt":"2024-08-01T14:46:37","slug":"template-inheritance-in-flask","status":"publish","type":"post","link":"https:\/\/geekpython.in\/template-inheritance-in-flask","title":{"rendered":"Template Inheritance in Flask with Example"},"content":{"rendered":"\n<p>What is template inheritance? A web application has a minimum of four to five pages. These pages contain common components like header, footer, background, etc. So, these components collectively put into a template for reusability.<\/p>\n\n\n\n<p>Other pages of the application inherit these templates, hence the process is called <strong>template inheritance<\/strong>. Let&#8217;s see how we can do the same in <strong>Flask<\/strong>.<\/p>\n\n\n\n<p>Every webpage has a header specifically a navigation bar and a footer displaying the business&#8217;s objective and important hyperlinks to different services.<\/p>\n\n\n\n<p>Let&#8217;s say we have a directory structure that looks like the following:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \" >flask_project\/\n|- app.py\n|- templates\n    |- base_layout.html\n    |- about.html\n    |- product.html\n|- static<\/pre><\/div>\n\n\n\n<p>We need to create a Flask app to serve our templates. Add the following code into the <code>app.py<\/code> file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >from flask import Flask, render_template\n\napp = Flask(__name__)\n\n@app.route(\"\/\")\ndef home():\n    return render_template(\"base_layout.html\")\n\n@app.route(\"\/about\")\ndef about():\n    return render_template(\"about.html\")\n\n@app.route(\"\/products\")\ndef products():\n    return render_template(\"product.html\")\n\nif __name__ == \"__main__\":\n    app.run(debug=True)<\/pre><\/div>\n\n\n\n<p>We have three templates that will be rendered on their respective URLs that&#8217;s why we created three routes (<code>\/<\/code>, <code>\/about<\/code>, and <code>\/products<\/code>) in our app.<\/p>\n\n\n\n<p>Here&#8217;s a simple navbar and footer using Bootstrap.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" >&lt;!-- base_layout.html --&gt;\n\n&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;title&gt;Template Inheritance&lt;\/title&gt;\n    &lt;!-- Bootstrap CSS --&gt;\n    &lt;link href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.3.3\/dist\/css\/bootstrap.min.css\" rel=\"stylesheet\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;!-- Navbar --&gt;\n&lt;nav class=\"navbar navbar-expand-lg bg-info\"&gt;\n    &lt;div class=\"container-fluid\"&gt;\n        &lt;a class=\"navbar-brand\" href=\"#\"&gt;Navbar&lt;\/a&gt;\n        &lt;div class=\"collapse navbar-collapse\" id=\"navbarNavAltMarkup\"&gt;\n            &lt;div class=\"navbar-nav\"&gt;\n                &lt;a class=\"nav-link active\" aria-current=\"page\" href=\"#\"&gt;Home&lt;\/a&gt;\n                &lt;a class=\"nav-link\" href=\"#\"&gt;Products&lt;\/a&gt;\n                &lt;a class=\"nav-link\" href=\"#\"&gt;About&lt;\/a&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/nav&gt;\n\n&lt;!-- Main Content of Page --&gt;\n{% block content %}\n&lt;h1&gt; This is Homepage &lt;\/h1&gt;\n{% endblock %}\n\n&lt;!-- Footer --&gt;\n&lt;footer class=\"bg-dark text-white\"&gt;\n    &lt;div class=\"container py-4\"&gt;\n        &lt;div class=\"row\"&gt;\n            &lt;p class=\"mb-0\"&gt;\u00a9 2024 Your Company. All rights reserved.&lt;\/p&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/footer&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>Now run the app and go to this URL <code>http:\/\/127.0.0.1:5000<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1365\" height=\"184\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image.png\" alt=\"Base Layout\" class=\"wp-image-1739\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image.png 1365w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-300x40.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-1024x138.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-768x104.png 768w\" sizes=\"auto, (max-width: 1365px) 100vw, 1365px\" \/><\/figure>\n\n\n\n<p>Our webpage now has a simple layout. The next goal is to avoid explicitly including the identical components (navbar and footer) on each page (<code>about.html<\/code> and <code>product.html<\/code>).<\/p>\n\n\n\n<p>As a result, we will use <code>base_layout.html<\/code> as our base template to include the navbar and footer on subsequent web pages.<\/p>\n\n\n\n<p>How&#8217;ll we do that? Of course, by using <strong>template inheritance<\/strong>. Let&#8217;s start things off.<\/p>\n\n\n\n<p>Here, we&#8217;ll use <strong>Jinja<\/strong> syntax to achieve our goal. In the <code>about.html<\/code> add the following code.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" >&lt;!-- about.html --&gt;\n\n&lt;!-- Base template layout --&gt;\n{% extends 'base_layout.html' %}\n\n&lt;!-- Main Content of Page --&gt;\n{% block content %}\n&lt;h1&gt; This is About Page &lt;\/h1&gt;\n{% endblock %}<\/pre><\/div>\n\n\n\n<p>This line (<code>{% extends 'base_layout.html' %}<\/code>) indicates that the <code>about.html<\/code> template will use the <code>base_layout.html<\/code> template as its base. This means that <code>about.html<\/code> inherits the structure and layout defined in <code>base_layout.html<\/code>.<\/p>\n\n\n\n<p>If you&#8217;ve noticed, we inserted the content block (<code>{% block content %}{% endblock %}<\/code>) in our base template, this will help us serve the dynamic content. This is what we&#8217;ve done after extending the base layout.<\/p>\n\n\n\n<p>Now, we can insert anything within the content block using the above syntax, in this case, we&#8217;ve inserted a simple <code>&lt;h1&gt;<\/code> tag.<\/p>\n\n\n\n<p>This is the result we got, <code>about.html<\/code> serving on <code>http:\/\/127.0.0.1:5000\/about<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1364\" height=\"183\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-1.png\" alt=\"About page using base layout\" class=\"wp-image-1740\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-1.png 1364w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-1-300x40.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-1-1024x137.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-1-768x103.png 768w\" sizes=\"auto, (max-width: 1364px) 100vw, 1364px\" \/><\/figure>\n\n\n\n<p>Similarly, we can do this in <code>product.html<\/code> as well.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" >&lt;!-- product.html --&gt;\n\n&lt;!-- Base template layout --&gt;\n{% extends 'base_layout.html' %}\n\n&lt;!-- Main Content of Page --&gt;\n{% block content %}\n&lt;h1&gt; This is Product Page &lt;\/h1&gt;\n{% endblock %}<\/pre><\/div>\n\n\n\n<p>This time we are serving different content. Here&#8217;s the result.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1366\" height=\"183\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-2.png\" alt=\"Product page using base layout\" class=\"wp-image-1741\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-2.png 1366w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-2-300x40.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-2-1024x137.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2024\/08\/image-2-768x103.png 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/type-hinting-in-python\">Type hints in Python &#8211; Callable objects, Return values, and more<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/positional-and-keyword-arguments-in-python\">Best Practices: Positional and Keyword Arguments in Python<\/a><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/yield-keyword-in-python\">Yield Keyword in Python with Examples<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/build-websocket-server-and-client-using-python\">Create a WebSocket Server and Client in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/mysql-database-in-python\">Create and Interact with MySQL Database in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/understanding-the-use-of-global-keyword-in-python\">Understanding the use of global keyword in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is template inheritance? A web application has a minimum of four to five pages. These pages contain common components like header, footer, background, etc. So, these components collectively put into a template for reusability. Other pages of the application inherit these templates, hence the process is called template inheritance. Let&#8217;s see how we can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1743,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[5],"tags":[36],"class_list":["post-1738","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flask","tag-flask","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1738"}],"version-history":[{"count":1,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1738\/revisions"}],"predecessor-version":[{"id":1742,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1738\/revisions\/1742"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1743"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}