Jinja2 Templating Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this video we're gonna be talking about that Jinja to templating engine so we're gonna be exploring what template engines do how to load the templates temp label z' temp like control and control structures and template inheritance so for some what do template engines do well there's a lot of ways of thinking about them but I kind of like to always explain them like a supercharged version of Mad Libs okay so here's a very short and simple example so you have these curly braces you may have seen these so it's double curly braces so it's named had a little name or it's go saves blank had a little blank that makes sense the yes hopefully okay so the next slide hopefully will clear things up if you don't get it so if name equals marry an animal equals lamb then our little little example here would be mary had a little lamb so python does have a really great built in string formatter and i already covered that in a previous video and i totally love it but occasionally you need a little bit more power or you know more power or also if you're if you're doing like a lot of text this is the formatting it's probably not the best way to go so yeah so if you have like large amounts of text in Jinja to is gonna be probably the best thing that meets your needs so Jinja to is modern and designer firmly templating engine for python while after Django's templates house from the docks most tipping agent most most templating most template engines are usually used in web apps but that's not necessary but we're gonna show you okay so here's an HTML example I've got this curly braces so I've got a title a header and a body but I just gonna say fright jumped all over myself they're not exclusively used for what they can be used for any kind of text it just happens to be that you know one most common things especially in Python than we do is during text that happens to be an HTML document so how do you load these templates if you're using Jinja - without web framework you're gonna be I'm gonna say just use whatever they recommend like if you're using flask or I don't know there's a bajillion of them I can only think of flask right now but there's dozens them use whatever method they tell you to right now yeah as stated earlier using Jinta - it's not exclusive to web frameworks so we're gonna be pretending like we're just generating text or not tied to a web framework so that way we can comp lower the amount of complexity so what we're gonna be doing is just having Python scripts that generate strings some of those strings all gonna be bigs I'm gonna short but just strings so yes in this video we will not be using web framework this reduce the complexity the code I should have read my slides one more time I jumped all over myself I'm so sorry there's no ways to load templates we'll be using the file system loader in this video all the code is example have this import statement implied view it it's wonderful okay so we're gonna pass a directory containing the templates to the file system loader right here so we're gonna do is I'm gonna make a or my code lives I'm gonna have a directory in there called templates and that's one of my templates are gonna be and we're gonna give the posted loader that name of that templates like if you wanted to call it um I don't know plates you just say give it plates I of course it just Nemo template it's gonna be a lot easier then we load the environment so what we're gonna do is we have this environment and we're gonna give it the oops the loader we created earlier so then now that use your two environments ready we make our first template inside of that template directory and we're gonna call HelloWorld dot txt because legally every single tutorial has to start with hello world and don't want to be in the wrong side of the law so in that hello world we put hello world that's but that's it okay so now we want to load that template so remember we create that environment that environment has a function called get template and then you give it a filename and then it's going to get that template look see I'm gonna put right here mm-hmm and then we can render it so that template so here we go we got we got the template and then we put into a template object and then that template object we called render to output and then we're not print out that output and what do you think is gonna happen guess oh it's hello world there we go okay so this is pretty only an extra string but it is the absolute simplest possible example and that's really why I want to show you I when I was first learning ginger - I was very frustrated with like well now I view as unneeded complexity like if someone hadn't showed me that it's much more clear that way so let's move into something more complicated what if we add a variable to this template variables okay so we create a new template called lambda X T what we're gonna do is have just remember those Mad Libs we did before the curly braces and yeah that's just a placeholder right so we got here curly braces name and it's had a little lamb can you guess what's gonna happen I bet you can so the curly braces in the name that is a template variable so now we can load the template just like we did with the hello world so we have the environment we're gonna call get template and then give it a file name and then it's gonna assign that to that template and now we want to render it this is where it gets interesting so we call that render function and then we give name equals Mary so that was a variable that we'd put into that template and between those crew the braces was named everything that we say here in this render when we say name that's who knows that's gonna equal Mary uh-huh I don't know if you saw that I almost broke everything so now we're gonna print that out what happens we printed out this mary had a little lamb' why is it say that well because we gave to the variable name and that variable name is right here in the crews brain so whatever you pass as that variable is gonna be put into the template so I had LEM and now if we want to change the name you know I've always wanted to have a farm and I've always wanted to have a sheep no sheep with lamb I think so I don't know enough to be a farmer but here we go so we're gonna use my name Jason and we're gonna put that out so where's that pronounced to be Jason had a little lamb if only okay now what if we add another variable so now we have our curly braces name had a little curly braces animals is this templates gonna take two variables name and animal and whatever the value of name is it's gonna be put where that name spot is whatever this value of animal is it's gonna be put that little kind of mole spot is so we're gonna pass motile variable so when we do that render we're gonna give name the value Bob and animal the value of cat okay print that out and what do you think is gonna happen well Bob had a little cat so sweet you know they say cats are really big on the internet so hopefully this will give me more hits so we can also get some more complex objects so here look at this so we've got a curly braces data dot name and then curly braces data animal okay that's we're getting to a little more complex objects so what we're gonna do is we're gonna pass that template we're gonna give it a dictionary so stiction airy it's gonna have a person gonna have a name Frank and an animal dog and now when we run into that template the data variable is going to be assigned the value of person so we're giving this data here this dictionary up here now we're print that out and was it say Frank had a little dog I'm sure he's very happy the dogs are very cute okay so template control structures so control structures control the flow of the program will cover conditionals and for loops there's a few more but that were just kind of covered basics just to get you running and playing though this important thing is after you watch this video just start playing just start trying it out and then you probably the how do that then Google is your friend conditionals so we create a template called truth txt now we've got something a little different is ever having those double curly bracket braces we've got I currently brace a percentage and then percentage and curly brace I kind of closed the statement so if truth then it will do this statement this is true more the curly brace percentage sign so they kind of see how they kind of balance each other there's one on this side or on that side so that this isn't going to be printed at all anything in between these curly braces and these percentage items is not going to be printed this kind of giving the logic of it so if you look so if we have a variable named truth and if that variable named truth is true it will print this out if it's not then it'll print this out and then we do end F to end this if looper well not if loop this if structure and then we're gonna render what render it with that variable just like we've done before with those other examples here so we make we would get our template we render it we give truth the value of true and we've printed out and it's gonna say this is true now what if we change to false false it's gonna say this this is false let's go back just so you can get one more look at it so hopefully this this makes sense here it's it's pretty straightforward alright now we're gonna go to for loops for loops are gonna be pretty pretty snazzy so we're gonna create a template called rainbow dot txt and we're gonna do is once again these are kind of lazily my kind of logic statements here were these percent curly bracket curly bracket percent then estate man than me okay whoever you get the idea I'm sure hopefully hopefully you do for the idea right okay so we're gonna do is have for each color and colors we're gonna print color woo I've broke it again my goodness I'm being reckless okay hopefully this this makes sense it's pretty self-explanatory so we're gonna do is we're gonna get that template rainbow sixty we're gonna give it colors these are not all the colors of the rainbow I tried if I did that it was too long just too big I had to make the fonts too tiny so in our world right now a rainbow only has a red green and blue sad world okay so we're gonna give that template the colors will the list of colors here anyway I'll put that and green red green blue why is that oh look we said for every color and color print color so it did that template inheritance okay this this is were thing yeah things are really fun here so check one template inheritance allows you to create building blocks that you can combine in this section we will be using HTML as an example just because it's so it's such a reasonable choice as an example so we're gonna make a template called header and the header is going to have this curly brace isn't it's gonna have a variable a template variable called title and here we have another template called base HTML and now we're gonna do a cool thing we can include some templates into other templates so we just made this one called head header doc to HTML we can include that once again what the special ones where it's a curly brace and percentage we can say include and then the name of the file we'd like to include and now we render it so we're gonna say the title is page title very very creative I'm always so creative and then we're print that out and it's going to do exactly like we thought but if you notice we didn't call and that render we called base HTML and then base HTML inherits header HTML so you can see how you can start building these you could have one you know called footer HTML or CSS HTML CSS it yeah okay so and now we will enable some child templates for basically makes 18 so here we go this is where things get weird a little bit but once you once you want to do this a few times it make sense so within the base HTML we're gonna just say there's this content section here okay see this this is this oh this is this line here block and block and then we've got content so now we make a template called child HTML and this child now hTML is going to extend base HTML okay so we say we say it extends base HTML and now earlier we had that block content now we re going to define that block content so here we go block and block content and now we have the body variable inside about parentheses parentheses paragraphs sorry and now we can render this new template so we're gonna call or get that template the child HTML we're gonna give it a title of page title and a body of stuff we're gonna print that out okay look look it works it works so you can see how you can cut there like building blocks and good book one inside the other and then one's a child or the other one it it can get very complex and yeah it's very very cool it also helps really separate things you can imagine if you were actually building a web page how nice it would be to stab to only deal with the stuff that's in the body everything else is not your problem right you've already taken care of all that like a lot of websites everything pretty much everything in this head here it's all going to the same on every page that site so you just always have that be inherited conclusion so in this video we learned what template template engines do how to load templates temp label some control structures and template inheritance now they cover the basics start coding some of the stuff I showed you might seem a little strange just start messing around with it once you start messing around with it it starts making sense change it to as many many more features it is really cool there's a lot fray to explore so just kind of poke around do some experimenting get comfortable with it and then just go wild good luck thank you for watching this video if you liked it please hit that like button down below and if you want to see more videos about programming and open source stuff software hit that subscribe button thank you so much for watching
Info
Channel: Mr. Rigden
Views: 88,271
Rating: undefined out of 5
Keywords: jinja2 tutorial, jinja template tutorial, jinja2 tutorial python, jinja2 template tutorial, python templating engine, jinja python tutorial, jinja2 template engine, jinja templating tutorial, python jinja2 tutorial, jinja2, jinja tutorial, jinja2 tutorial for beginners, jinja templating, jinja2 template, jinja2 template from file, templating engines, jinja tutorial for beginners, python jinja2, templating, templates, template, jinja, flask
Id: bxhXQG1qJPM
Channel Id: undefined
Length: 16min 23sec (983 seconds)
Published: Tue Mar 13 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.