Jinja Python - Fast Expressive Templating Engine - Advent of code Jinja2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another episode of invited entry it is day four which means we are doing another library another python library um not my favorite python library in the world i'm afraid but super powerful i like this library because i often use django as my a web application framework of choice that has an inbuilt templating language uh ginger or ginger too to be precise again by the palace project which is dangerous is by as well is super powerful for doing templates so the way it works uh we'll get straight into it and i just want to make sure i have my crib sheet open on the other screen is a little bit hard to set up than the other library so you're doing the from ginger to import environment file system loader uh i think we need to select auto escape escape as well so what we have to do is the environment we'll end the environment um now you can use other loaders i think they recommend using the package loader if you're building a web app you probably use a package loader so you can separate your packages and your templates that go with the packages um i'm gonna use the file system loader i put all my templates in one place and auto escape door equals select auto escape so that's basically my setup to begin with what i have here is i have a folder called templates inside the template i have a html file which is a very basic html file and i shouldn't have clicked that uh what i should done was click that to edit it so as you see in the editor and it's got this very special thing in the middle which are two braces and the word thing and this is where it kind of looks a bit like um a format a string format you might say well if i've got a very simple template it doesn't matter the power comes later on when we start using some of the modules and features so the first thing we do is we make a template object so env.gettemplate1.html and this will search the fast the load as it's got loaded to where that is and we'll produce a template object so what we can do is we can render data to that template so template render um now the word here was thing was thing in that i don't want that open at all um so i'm going to pass the argo a thing equals hello there so when that actually runs it replaces those tags with the things that i pass in so if that was called uh wibble then i'd say wibble equals there uh if you're from the django world and you like contexts which are just dictionaries and you'll do thing equals hello there and then because it's a dictionary it will be occurring i just want to make sure i haven't gone behind my own screen as i would type uh i'm close to it it's still good and then what you would do here is you'd still do your template.render but at this point you would do asterix asterix um asterix ashtix context to pass that as keyword arguments and it would still work um we will remember is if you change your template you have to reload it because that's actually loaded and effectively compiled the template at that point um so i'm just going to put it on top of that it's going to rewrite this and the power this is that we can then start doing things like array so maybe do a database query and it comes back with a bunch of uh objects it could be other dictionaries so name equals wibble uh count equal a is equal to before with five i want a whole bunch of these um i'm just gonna change them a little bit ah okay so when i run this nothing really changes the output in my template what i can now do because i'm making html templates here by in mind i can make any text template so i can make pdfs uh i can start injecting things into pdf anything i like really in this which is a text based language we now use a different type of tag which is a brace percentage and we do four row in data that's the name of the variable and four if you have an ide there are ginger plugins to visual studio code probably pie chart things as well um so here what i'm going to do is i'm going to start my table outside i'm going to end my there's probably table header stuff like that but you know i don't care about um row make sure that the template picks up row dot name row dot count okay count save that go back to my ginger notebook and run this again and you now see i've now got that repeating row in here okay so this is now using a second language the ginger templating language to produce higher quality templates uh if we go a little bit further we can start doing cool things like um if um oh pardon me again i'm gonna look at my crib sheet to do it right first time because this time i actually made a crib sheet but i got rid of my crib sheet so that shows you how useful that crib sheet actually was one thing you can do you can say um i've forgotten the expression and the reason i forgot the expression is because um i often i often do it in django and it's a little bit different here so because this is a loop there's a special variable called loop so update index is divisible d visible by i don't know three two three percentage out then what i would do notice i'm inside the tag here i can then say class equals uh different color whatever my class name is i want to close my if off that so this if it's only going to introduce a different color on every third iteration so i can save that run my book again which is going to reload the template at the top here and what you now see is that every third one so one two third one has a different color four five six one has a different color working perfectly and you can start filtering these things you can start doing things like adding in a capitalization or particular spacing uh of of the data so you're separating out the business logic of what data to show so the logic here thinking about concentrating your efforts the logic here will be about making the correct query on the database or processing the information where the the the code here is about how you display that information to your end user and it gives you a better quality output you can go further on this as well what you can actually do is you can use templates to do cool things and this is where you decide how your implementation wants to work there's two main ways of doing this one is to have a base template like this and then have an area for the main body so you might have what's called a block so here we block um like that this block you can put some default content in there and then what you can do is insert templates you can have a new text file called to dot html and this will extend one.html i don't have anything i just block main body and then end lock and here i might do my code which i did previously which was um i think what was my what's my top variable called thing there we go thing this at this point calling the one html will give us the default content calling the two will give us a syntax error because i've done it wrong and the reason i'm doing wrong is because i don't extend and it should be extends like that there you go all right and i'll explain why i didn't get that wrong in a second um apparently i've still got it wrong one but so this should be in quote marks c practice beforehand before you do your once through recordings there you go and you notice here that it's now pulled in the base template so you might use your base template to define headers footers maybe the sidebar goes in here that's one way of doing it the other way of doing it is that you may have a template you want to use but you have some kind of special sidebar so instead of this here you can actually do an include sidebar dot html that there's a bit of a weird lag there and what that would do is it will grab that sidebar and then copy and paste it in you can use that if you've got repeatable snippets that you want to use throughout your pages where you didn't have a common base perhaps so a combination of both techniques is really powerful um i love ginger i love the django templating i believe ginger was based on the django templating but i may have that backwards um using the different filters using the difference between moment differencing uh brace percentage and brace brace is really important i think when i started out getting those two backwards was something i did an awful lot of um and the other thing that you should get into the hand of is filters um so there is a bunch of standard filters and i will try and find one of them now the django ones are different to the other ones there's one here for example which is word wrap so with filters you can basically pipe a variable through and then change what it looks like so it is worth looking at the documentation it is worth looking at at how you can use these various things you kind of need individual um use cases to go to look at these so you can look at absolutes you could say if i have a number i actually want to change how how many decimal points so if you're outputting like scientific outputs that's gone five or six decimal points you say no i only want to show two so you can filter that in the in the visible template layer not in your business logic calculation layer it also helps if you're doing lazy stuff so if you're lazy loading from a database then you don't want to iterate over the data here to manipulate data and then manipulate it literally over again in your template you can do visible stuff in the template cylinder video today because there is a lot in this this is a templating language if you're not using templates at the moment you really should be there are different versions of this there are other templating solutions out there i use this for everything from web pages to generating emails to generating uh texts i generated but i've got a complicated text structure or output uh for console i sometimes use a template um i use snippets there's all kinds of things um and i absolutely love ginger it's a very lightweight library so if you don't want the full power django if you're maybe writing flasks you should use templates like this ninja templates are the way to go so i fully recommend this just before i sign off today i want people to remind that actually it is team c's um i am just in the end of the crypto phase of team scenes here but here's team c's.org uh they are currently trying to remove 30 million pounds which means raising 30 million dollars by the end or by the first of january and they're currently at and wow my computer is running slow at the moment they're currently somewhere in the region of 16 or 17 million dollars which is an amazing figure well over halfway we're just past the halfway point as well so if you haven't yet i would recommend sending the price of a cup of coffee some of that to team sees because they are going to actively remove plastics from the world ocean and that will have a measurable impact on the future of this planet so please please please if you haven't already please make its team seats thank you very much
Info
Channel: InvalidEntry
Views: 8,542
Rating: undefined out of 5
Keywords: python tutorial, jinja template tutorial, jinja2 template tutorial, python jinja2 tutorial, jinja2 tutorial, python3, python 3, python, learn python, jinja tutorial, jinja templating, jinja2 template, python jinja2, jinja2 tutorial for beginners, jinja2 template from file, jinja python tutorial, jinja2 tutorial python, python templating engine, jinja2 template engine, jinja templating tutorial, python programming, python (programming language), templating engines
Id: NR-BGPhDICA
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Sat Dec 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.