Golang Templates

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video is brought to you by Greater Commons greater learning greater opportunities greater life visit us at grater Commons comm we have a basic 3 page web page going but this is crappy all of our code all of our HTML right in there with all of our go that's not good organization from programming it's gonna get messy this page here is designed for go code and we have other pages which are designed for each team else which keep our HTML stuff on one page or go stuff in another so that's what we're gonna do when we use templates I'm going to show you what templates are but a template will just give you a little description the templates like a form letter where it's like insert first name here insert last name right and then you run that against a bunch of data pieces of data like bunch of records in database and it prints out one letter for Todd one letter for James one letter for John right and just fills in the data where appropriate it's a template does that make sense that's what we're gonna do we're gonna learn how to use templates and so I'm gonna copy this folder here 12 I'm gonna make it 13 and then inside there I just have this temp folder to file we don't need that anymore man get rid of that alright so now I'm going to create a new folder templates a new directory called templates and in templates I'm going to create a file just a regular file regular file and that file is going to be indexed go HTML so you could give this an extension not given an extension you can call it dot PHP called ASP you can call it dot PHP ASP you can call it dot my grandmother's house call whatever you want it doesn't matter this is just programming you're not operating within the restrictions of a lot of the other languages this is like a file you're going to open the file you're gonna read the contents of the file and you just tell the program hey this is the name of the file and they'll do it okay cool so go hTML is the convention though okay for templates so index.html and then I could use Emmet just exclamation point in tab and I've associated my editor to know that go HTML files are each email files and so it drops all that HTML in there and then index right and and then I could put here you our index and then I could do an anchor tag and this one will go to about and then I want one to be index and and then I want one to be contact so this is like what we had before if I'm just putting it all here now I could copy this copy paste it and I forgot to put in my brakes VR VR and I'm going to copy that and paste it and just call this one about and copy that again and paste it and call it contact and then I'll change about contact this one's contact and I'm just you know changing me so it shows us which we're at and about and about all right so we've got index we've got about and we have contact cool close them all those are my templates and then over here in my code I'm gonna get rid of all this stuff so I'm gonna just highlight all that and bye all right that's where we had to HTML before and highlight all this and by and highlight all this and by and so just take a moment look at this you're watching this video online and by the way if you want to find our code you go to get github going what goes to Levin going web dev there's the URL and we're working out of zero zero temp 46 spring 17 is where we're at in going web - and by the way you probably want to follow me on Twitter if you're not doing so already because I am all about go strangely enough this is what I do for a living and I love it and it's pretty cool that I ended up here and I don't know why Twitter's not coming up Twitter's busted so here here I am on Twitter and that's where you find me on Twitter and like today I put up this cool thing about concurrency and parallelism 13 hours ago and guess which one is Windows that's pretty fun the one with the banished screen of death so it's fun all right so uh now to do templates let me think about this well we were talking about it so if you're watching this video online just pause this here for a second just look at this and if you're in the classroom look at it let's make sure we understand it all right so we want to use templates and over here at the documentation there's text template and so that's one place where we see documentation for templates you can see I'm a packaged text template but there's also HTML template and you might think well hell I want to HTML template you do you want to HTML template but everything in HTML template is built on top of text template so HTML template package HTML template is everything that text template is plus stuff for HTML alright and so to understand how to use templates we look to both text template package and we we look and see okay how does that work here and then we also look at HTML template all right so here we have templates parse files parse glob and we parse glob would give it a pattern and it gives us back a pointer to a template and an air and with a pointer to a template we have all these methods as look there's the receiver right pointer to a template so if you have a tight pointer to a template a value of type pointer to template you have all these methods importantly we have execute template right here and and so that that's that's all very helpful if we go and we look at HTML template that's a text template we can see a little bit about HTML template I'm down look at the index same stuff down here right where we have a parse glob gives us a pointer to a template in there and we have execute template and there's template must write must and so I'll show you how that all works so there's another function that's a primary function and that's called a knit and I that's my word primary function primary in the sense like mains pretty primary that's where your program runs an it is going to do any setup for your program it initializes your program and and I'm going to create a variable up here I'm going to call that variable TBL and it's going to be of type template from package templates so this is type pointer to template from package template and out saying hey you want to import this type and I do and I want to grab it from HTML template so it just brought in this package and now I'm going to use code from that package and I'm you I'm creating a variable TPL which is uptight template pointer to a template from package template okay and so that's that's my variable here and here's type template and it's gonna be a pointer to a template because that's what you work with okay see it is yours learning kind of how you set it up and now I want to assign to this and you can think of this a value of type pointer to a template like a bucket just like where all of your templates gets stored so TPL is equal to template must okay and then I'm going to do template parse glob and so I'm using parce claw right here and it takes a pattern and gives me back a pointer to a template and an error so I'm going to give it a pattern and my pattern is going to be in two and that pattern is a string so I'm going to go into from my root which is this is my root area go into templates I get try it like this go into the templates folder and and then start go HTML parse a glob is like a bucket jump if you look up glob that's like a comp side term glob define comp side blob programming Wikipedia in computer programming in particular in unix-like environment glob patterns strictly sets specify sets of file names with wildcard characters so glob is bunch of file names for example UNIX command move star dot text this area lose all files names ending in txt from the current directory all right so stars the wild car standing for any string of characters and start out text is a glob pattern so it's no surprise that they're using stuff like this because the guys who created go are also some of the same guys developed to create UNIX whoa all right he created you TF a UNIX and C these are heavy hitters and so here we have template parse glob and we pass in the pattern and that gives us back that gives us back a pointer to a template in an error okay let me see if I can get this on the same screen so template template from package template parse cloth package template parse glob and I give it a string what does that return what's a return returns to values of what type parse glob returns to values of what type pointer to a template in an air this is going to return right here a pointer to a template and in there what is must take what is must take as arguments point must takes a pointer to a template and an error well parse glob returns a pointer to template in air so this runs returns a pointer to whoops returns a pointer to a template and an error which is just exactly what must was and the must just gives me a pointer to a template so must is a helper function that checks my air if I have an air it just shuts my program down we're done all right here must is a helper that wraps a call to the function and panics if the air is non-nil shuts me down that's what pan of kids like shuts me down so I'm able to do parse glob and then must and then assign that to TPL and I put this up here so it's a package scope so I can now use that all the way throughout my program so instead of this line TPL execute template because when i have a value of type pointer to template I have execute template which takes a writer the name of the template and any data and returns in there so template it takes a writer the name of a template and any data I'm gonna pass it and now I'm going to copy that line I'm down here and paste it I'm down here and paste it and this one's going to be contact but good HTML and this one's going to be about not go HTML and that corresponds to those three files I'm saying you know what you know and look at how much cleaner that is and having all my junk HTML on the same folder this page here is designed to work with go code so I can get all my go formatting and linting and code completion and these pages are made to work with HTML so get on the HTML help and Amit and and so but now right Bharti pl up at the package level scope so it's available to all these functions and then when I initialize my program runs I parse all of my files parse just means kind of go get them and get them ready read them into the program I sign that to TPL and then down here I say execute a template execute index go HTML and write it here execute about and write it here and so when I run this it's all small again you guys see that who's your daddy questions if you want to incorporate CSS like an external style sheet that you have do you link to the style sheet in the code document or do you have to so that to include CSS as an external stylesheet there's more involved answer because now we're serving files right we're not just doing a template where our go program parses the template says here it's ready to be used we're now serving files because our you know our will will the execute template serves that template to the client the taught client gets that template and then in that template there's going to be something like this which is right and it'll be like something like that and it's gonna say give me that file now we have to serve a file back so we have to learn all about file servers before we do that okay so for for now all of your CSS here until we learn how to serve files and that's just a big line chunk of code that you'll be like okay I'll just copy and paste that in good question great question so if I wanted to here I could put in you know HTML and font size 48 pixels and I can make all that big and drop that into each of mine control-c stop it up error to restart it enter a pair for the last command enter come back here command shift are to refresh what it's doing - quick before some people are like how was yours automatically refreshing as like that's me do it quick how many people think that's awesome you want to take it to the next level awesome so before I do that and I'll do the next level of Awesome in the next video but before we do that you need to code all this by next Wednesday so a little bit of a review step number one listen and serve right and your your your tools so I'll just do a little review here new directory 14 a new file main main main step 1 HP listen and serve and then it tells me you need an address and a handler call it 8080 and no ok and then I'm just going to do HP handle funk and I give it the pattern and the function and that function needs to have a response writer and a pointer to a request because web servers receive requests and they write responses and this font this function index is what will handle this route which is why it's called a handler this is the code that's going to handle this route it's the handler it's like some Lucas on movie call the handler you know Lucas on movies oh dude you've never watched with him Nikita best move I'm gonna show you the trailer that a little fib Nikita's soon as we're done here it's like so we just did that right and then we have to set up our tip so you guys make a flash cars a stack of flash cards and put one two three autumn step one is HP listen answer so front side step one back side HP listen and surf step two h-2b handle func right and the route and then the function name step three a function with response writer and a pointer to request and this is all from package HTTP that's three steps write them on index cards I want you to come in and be able to hard-code this from nothing practice it every day what's up route and a function and that function is step three where it takes response writer and a pointer to request step four is VAR TBL pointer to type template from package template type template from package template and we're going to bring in HTML template okay step 5 is funkin it it runs at the beginning of your program once initializes your program step 6 is T PL is equal equal to template must template parse glob pattern it's from my template folder and it's a star go HTML that's step 6 and so then I need to create my folder over here step seven TPL dot execute template writer index dot go HTML and nil cuz I'm not passing in any data so in here I didn't put in suzune time index go HTML emit exclamation point tab index and here you are at index and maybe a little bit making it bigger control C to stop it change directories there are in Maine so like seven steps got to know it by next class bring in the flashcards I want to see him thank you okay and every day when you wake up and you're brushing your teeth look through them when you're eating lunch look through them when you're going to bed and you brush your teeth look through them when you're waiting for a class to start look through them waiting for the boss look through them when you're pooping look through them everybody poops it's a good time to look through okay you're not pooping you better go see a doctor Rome that there's nothing wrong if you don't [Music] what about Maine no you're looking at all right I'm gonna blow your mind in the next video or show you something cool you
Info
Channel: Learn To Code
Views: 3,796
Rating: 4.8235292 out of 5
Keywords: go programming language, templates, text template, text/template, html template, html/template, website, web page
Id: YAEvDrrQZGM
Channel Id: undefined
Length: 23min 14sec (1394 seconds)
Published: Thu Feb 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.