{"id":8472,"date":"2020-09-14T16:08:35","date_gmt":"2020-09-14T16:08:35","guid":{"rendered":"https:\/\/www.askpython.com\/?p=8472"},"modified":"2021-06-24T01:09:10","modified_gmt":"2021-06-24T01:09:10","slug":"flask-templates","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/flask\/flask-templates","title":{"rendered":"Flask Templates &#8211; Set up Templates in Python Flask"},"content":{"rendered":"\n<p>Hey Guys!! Welcome to another tutorial of our Flask series. In this article, we will learn about Templates in the <a href=\"https:\/\/www.askpython.com\/python-modules\/flask\" class=\"rank-math-link\">Flask web framework<\/a> and how to use them. So let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are templates?<\/strong><\/h2>\n\n\n\n<p>Templates are the HTML, CSS, JS files that are used to display content on the website. Templates beautify the web pages and make them presentable. <\/p>\n\n\n\n<p>Hence all the websites comprise of Front-end (consisting of Templates) and Back-end(Flask framework codes and applications)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Web Templating System<\/h2>\n\n\n\n<p>A web templating system comprises a template engine, data source, and a template processor.<\/p>\n\n\n\n<p>In many cases, the website also displays data from the DB on their webpages. Web  Templating Systems do this. It combines the data from the file\/DB and the HTML (using Template languages) and then displays it on the webpage.<\/p>\n\n\n\n<p><strong>The exact working of a web template system is as follows:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Extracts required data from DB<\/li><li>Combine the Data into the HTML file(using the template language) with the template engine<\/li><li>Template Processor then processes it and outputs the resulting template file<\/li><\/ol>\n\n\n\n<p>Flask uses <strong>Jinja2<\/strong> as its default<strong> templating engine<\/strong>.  We will look at it further in the next section.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Jinga Templating Language (JTL)<\/strong><\/h2>\n\n\n\n<p>The template engine provides a template language with which we can add data into the HTML files. <\/p>\n\n\n\n<p>Jinja2 is a modern and designer-friendly templating language for python, modeled after Django\u2019s Templates.<\/p>\n\n\n\n<p>We will now see the syntax of this template language. It consists of 4 types:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Types<\/th><th class=\"has-text-align-center\" data-align=\"center\">Syntax<\/th><\/tr><\/thead><tbody><tr><td><strong>1) Statement Tags<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>{% %}:<\/strong> {% if&#8230;..else %} &#8211; {% endif %}<\/td><\/tr><tr><td><strong>2) Variable Tags<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>{{  }}:<\/strong> {{ variable }}<\/td><\/tr><tr><td><strong>3) Commenting Tags<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>{#&#8230;..#}:<\/strong> {# comment &#8230;.para #}<\/td><\/tr><tr><td><strong>4)Line Comment Tags<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>#:<\/strong> #comment line<\/td><\/tr><\/tbody><\/table><figcaption>Jinja2 Templating Language<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Adding Templates in our application<\/strong><\/h2>\n\n\n\n<p>Flask searches for templates stored in a folder named &#8211; <strong>templates<\/strong> located beside the main application file. So create the folder before we move on to the next section.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/image-16.png\" alt=\"Templates Folder\" class=\"wp-image-8475\"\/><figcaption>Templates Folder<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"316\" height=\"190\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/image-15.png\" alt=\"Image 15\" class=\"wp-image-8474\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/image-15.png 316w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/image-15-300x180.png 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><figcaption>Contents of the templates folder<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Render_template()<\/strong> function<\/h3>\n\n\n\n<p>Flask application renders templates using the function render_template()<\/p>\n\n\n\n<p>The syntax is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nrender_template(&#039;&lt;template_file_name.html&gt;&#039;, variables = &lt;values&gt; )\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Coding our Flask app<\/strong><\/h3>\n\n\n\n<p>Add the code into your file flask main file (<a href=\"https:\/\/www.askpython.com\/python-modules\/flask\/create-hello-world-in-flask\" class=\"rank-math-link\">see introduction to flask<\/a>)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom flask import Flask, render_template\n\napp = Flask(__name__)\n\n@app.route(&#039;\/blogs\/&lt;int:id&gt;&#039;)\ndef blogs(id):\n    return render_template(&#039;blog.html&#039;, number=id)\n\napp.run(host=&#039;localhost&#039;, port=5000)\n<\/pre><\/div>\n\n\n<p>Create the template <strong>blog.html<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;html&gt;\n    &lt;body&gt;\n        &lt;h1&gt;This is a Blog Webpage&lt;\/h1&gt;\n        &lt;h2&gt;Blog {{number}}&lt;\/h1&gt;\n        &lt;h3&gt;Test Blog&lt;\/h1&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p><strong>Notice<\/strong> how we used the variable tag of jinja2 language.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Running the Flask application<\/strong><\/h3>\n\n\n\n<p>Run the server and hit the URL<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"626\" height=\"313\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/image-14.png\" alt=\"Blog 2\" class=\"wp-image-8473\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/image-14.png 626w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/image-14-300x150.png 300w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><figcaption>Blog 2<\/figcaption><\/figure><\/div>\n\n\n\n<p>Voila!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>That\u2019s it, guys, for this article! This was all about Flask Templates. Try doing the above examples on your own for a better understanding. Till then, Happy coding!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey Guys!! Welcome to another tutorial of our Flask series. In this article, we will learn about Templates in the Flask web framework and how to use them. So let\u2019s get started! What are templates? Templates are the HTML, CSS, JS files that are used to display content on the website. Templates beautify the web [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":8478,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-8472","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flask"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=8472"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8472\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/8478"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=8472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=8472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=8472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}