{"id":3182,"date":"2020-08-25T04:23:40","date_gmt":"2020-08-25T04:23:40","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=3182"},"modified":"2021-01-20T05:00:10","modified_gmt":"2021-01-20T05:00:10","slug":"jinja-web-template-engine-in-python","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/","title":{"rendered":"Jinja &#8211; Web Template Engine in Python"},"content":{"rendered":"<p>Templating&#8217; engine is an engine that can manipulate your HTML code from the server-side by using the server-side code. A <strong>template engine<\/strong> is quite popular in frameworks. In the template engine, we can replace variables with the values sent from the source and use it as an HTML file.<\/p>\n<p>Jinja is a popular template engine of the python programming language. Jinja is basically used by Flask as the templating engine.<\/p>\n<h2>To install jinja &#8211;<\/h2>\n<p><strong>pip install Jinja2<\/strong><br \/>\nBasic Template Example of Jinja &#8211;<\/p>\n<p><code><br \/>\n&lt;!DOCTYPE html><br \/>\n&lt;html lang=\"en\"><br \/>\n&lt;head><br \/>\n    &lt;title>My Python Webpage&lt;\/title><br \/>\n&lt;\/head><br \/>\n&lt;body><br \/>\n    &lt;ul id=\u201dusers\"><br \/>\n    {% for user in users %}<br \/>\n        &lt;li>&lt;a href=\"{{ user.website }}\">{{ user.name }}&lt;\/a>&lt;\/li><br \/>\n    {% endfor %}<br \/>\n    &lt;\/ul><\/p>\n<p>    &lt;h1>Important Info - &lt;\/h1><br \/>\n    {{ info }}<\/p>\n<p>    {# a comment #}<br \/>\n&lt;\/body><br \/>\n&lt;\/html><br \/>\n<\/code><\/p>\n<p>There are a few kinds of delimiters. The default Jinja delimiters are configured as follows:<\/p>\n<ol>\n<li>\n{% &#8230; %} is used for Statements<br \/>\nE.g. {% set text = \u2018Hello Word\u2019 %}\n<\/li>\n<li>\n{{ &#8230; }} for Expressions to print the value.<br \/>\nE.g. {{text}}\n<\/li>\n<li>\n{# &#8230; #} are used for Comments which are not needed in template output<br \/>\nE.g. {# This is an example of jinja template #}\n<\/li>\n<li>\n#  &#8230; ## for Line Statements<br \/>\n# for item in seq:<br \/>\n    \u2026<br \/>\n# endfor\n<\/li>\n<\/ol>\n<p><strong>Example &#8211; How to input values from python to jinja template &#8211;<\/strong><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; <strong>jinja_generate_html.py<\/strong> &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>from jinja2 import Environment, FileSystemLoader<\/p>\n<p><code><br \/>\nimport os<\/p>\n<p>root = os.path.dirname(os.path.abspath(__file__)) #get the absolute path of the python file.<br \/>\nenv = Environment(loader=FileSystemLoader('.'))<br \/>\ntemplate = env.get_template('template.html')  #Get the template<\/p>\n<p>filename = os.path.join(root, 'output', 'index.html')  #adding path in root<\/p>\n<p>with open(filename, 'w') as fh:                      #open file output\/index.html in write mode<br \/>\n#render the template i.e. replace variable with the passing values and write it to the index.html with the updated values<br \/>\nfh.write(template.render(<br \/>\n        my_string = \"Hello User\",<br \/>\n        my_list    = [\"JAVA\", \"Python\"],<br \/>\n    ))<br \/>\n<\/code><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<strong>Template.html<\/strong> &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p><code><br \/>\n&lt;!DOCTYPE html><br \/>\n&lt;html><br \/>\n  &lt;head><br \/>\n    &lt;title>Jinja2 Example&lt;\/title><br \/>\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><\/p>\n<p>  &lt;\/head><br \/>\n  &lt;body><br \/>\n    &lt;div class=\"container\"><br \/>\n      &lt;p>Title : {{my_string}}&lt;\/p><br \/>\n      &lt;p>Value from the list: {{my_list[3]}}&lt;\/p><br \/>\n      &lt;p>Loop through the list:&lt;\/p><br \/>\n      &lt;ul><br \/>\n        {% for n in my_list %}<br \/>\n        &lt;li>{{n}}&lt;\/li><br \/>\n        {% endfor %}<br \/>\n      &lt;\/ul><br \/>\n    &lt;\/div><br \/>\n  &lt;\/body><br \/>\n&lt;\/html><br \/>\n<\/code><\/p>\n<p><small><em>If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level <a href=\"https:\/\/www.studysection.com\/php-programming-foundation\">PHP certification exams<\/a> are offered by StudySection along with other programming certification exams. <\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Templating&#8217; engine is an engine that can manipulate your HTML code from the server-side by using the server-side code. A<\/p>\n","protected":false},"author":1,"featured_media":3183,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[526,33],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - Jinja - Web Template Engine in Python<\/title>\n<meta name=\"description\" content=\"Templating&#039; engine is an engine that can manipulate your HTML code present in HTML file from the server-side by using the server-side code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - Jinja - Web Template Engine in Python\" \/>\n<meta property=\"og:description\" content=\"Templating&#039; engine is an engine that can manipulate your HTML code present in HTML file from the server-side by using the server-side code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-25T04:23:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-20T05:00:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/jinja.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Jinja &#8211; Web Template Engine in Python\",\"datePublished\":\"2020-08-25T04:23:40+00:00\",\"dateModified\":\"2021-01-20T05:00:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/\"},\"wordCount\":226,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Jinja\",\"Python\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/\",\"url\":\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/\",\"name\":\"StudySection Blog - Jinja - Web Template Engine in Python\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2020-08-25T04:23:40+00:00\",\"dateModified\":\"2021-01-20T05:00:10+00:00\",\"description\":\"Templating' engine is an engine that can manipulate your HTML code present in HTML file from the server-side by using the server-side code.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Jinja &#8211; Web Template Engine in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - Jinja - Web Template Engine in Python","description":"Templating' engine is an engine that can manipulate your HTML code present in HTML file from the server-side by using the server-side code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - Jinja - Web Template Engine in Python","og_description":"Templating' engine is an engine that can manipulate your HTML code present in HTML file from the server-side by using the server-side code.","og_url":"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2020-08-25T04:23:40+00:00","article_modified_time":"2021-01-20T05:00:10+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/08\/jinja.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Jinja &#8211; Web Template Engine in Python","datePublished":"2020-08-25T04:23:40+00:00","dateModified":"2021-01-20T05:00:10+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/"},"wordCount":226,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Jinja","Python"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/","url":"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/","name":"StudySection Blog - Jinja - Web Template Engine in Python","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2020-08-25T04:23:40+00:00","dateModified":"2021-01-20T05:00:10+00:00","description":"Templating' engine is an engine that can manipulate your HTML code present in HTML file from the server-side by using the server-side code.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/jinja-web-template-engine-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Jinja &#8211; Web Template Engine in Python"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":421,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3182"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=3182"}],"version-history":[{"count":3,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3182\/revisions"}],"predecessor-version":[{"id":3861,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3182\/revisions\/3861"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/3183"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=3182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=3182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=3182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}