Python and Flask - Understanding Templates and Jinja

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys over here and welcome back to the Codex in this video we're gonna go ahead and continue our Python and fast series learning about templates in flask let's get started now currently all of our websites are returning plain text the about page the blog page this is the blog post number however when you actually create your very own fast web site guys you want to go ahead and render HTML content again HTML is a syntax used to create websites very common very intuitive very easy to use and the best part is is using something known as tax so you have the HTML tag the head tag the body tag etc if you're not familiar with HTML definitely check out my HTML series otherwise let's continue on so right now I'm gonna go ahead and redefine the blog page to be made up of its very own blog HTML code and in this scenario I'm gonna go ahead and just write some rudimentary HTML code starting off with the HTML tag and closing the HTML tag going on with the head tag and the body tag so again some very basic stuff guys I'm not trying to make it too complicated but this is going to be our blog page and then inside of our body tag I'm gonna go ahead and give it a header welcome to this blog exclamation mark and then a paragraph tag I am avi the author of this block so again all I'm doing here guys is I'm writing the HTML content that should be rendered by flask in the slash blog destination and the reason why I'm doing this is because you're never really gonna render just one simple sentence so one simple text it's always going to be an HTML page and in this area I'm just hard-coding this right over here in the return statement so go ahead and save this and let's go ahead and run our website and it says all right server already in use so let me go ahead and close the server and run it again there we go and let me go to the blog and we see over here welcome to this blog I'm avi the author of this block fantastic so what flask did guys is a read this entire statement and realize it was HTML code and then rendered the HTML code now a better way we can do this guy's is by using a concept known as templates templates are Bey blueprints of HTML code that you can use and render in specific destinations now the way we can set up our very own template it's like right-clicking on our folder creating a new Python directory and I'm gonna call this templates make sure you do call it templates that's very important and inside of this I'm gonna create a new HTML file called blog HTML now inside of this I'm gonna say blog as my title and then inside of the body I'm gonna go ahead and copy paste the two lines we had over here command c and blog the HTML command v awesome so what I just did guys is I moved my HTML content from the return statement to the blog html5 now what I'm gonna go ahead and do is remove all this content as we don't need it and at the very top you're gonna notice I added something from flask import flask comma render template to actually render this HTML file we need the function render underscore template so make sure you go ahead and import this from flask once you go ahead and do that we're gonna go ahead and serve here render template and PLAs pass in blog dot HTML fantastic job let's go ahead and restart our server and refresh there we go welcome to this blog I'm avi the author of this blog now nothing really changed because it's the exact same content let's go ahead and change this up a bit I'm I'll be nosh go ahead and save and restart the server and I'll refresh and there we go I'm avi nosh so now we know that the template is currently being rendered and I know what you're wondering what is the point of templates well flask has this amazing tool called Jinja Jinja 2 is the template engine that comes bundled with the flask framework and it allows us to input in variables and data into our template a very quick example before I end this video guys let's go ahead and go to serve it up high and inside of this I'm gonna go ahead and say blog dot HTML author is equal to Bob so in this scenario I'm passing in a variable into the render template function creating a variable called author and giving it a value Bob and inside of the blog that HTML wherever I want to render this variable I'm gonna use double curly brackets that is the Jinja syntax I'm gonna say author so again author is a variable that I'm passing in into the brand new template function and it will recognize that I'm using author over here and passing that value so now if I go ahead and save this and restart a server and refresh I am Bob the author of this block so that is the power of ginger guys again the syntax for ginger is these double curly braces and then placing the variable name inside we're gonna play a lot with this in future videos I hope you're excited this was a very brief introduction into templates how you can set up a template and render it and flask don't forget to Like share subscribe and I will see you in the next video [Music] [Music]
Info
Channel: TheCodex
Views: 37,173
Rating: undefined out of 5
Keywords: python (programming language), python flask, flask tutorial, python flask tutorial, flask (software), python flask tutorial for beginners, flask framework, python framework, flask tutorial for beginners, python tutorial, python web framework, flask web framework, python web, python web development, flask project, python flask from scratch, flask python example, thecodex, avinash jain, flask templates, python flask template, python flask html, flask html, Python Jinja
Id: NKJV0ekmo4U
Channel Id: undefined
Length: 5min 12sec (312 seconds)
Published: Sun Jan 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.