Press "Enter" to skip to content

HTML templates – Go Lang Practical Programming Tutorial p.16


what is going on everybody welcome to

what is going on everybody welcome to part 16 of the go language tutorial

part 16 of the go language tutorial

part 16 of the go language tutorial series in this part what we’re gonna be

series in this part what we’re gonna be

series in this part what we’re gonna be talking about is templating with gos

talking about is templating with gos

talking about is templating with gos HTML slash templating package so what

HTML slash templating package so what

HTML slash templating package so what I’ve got up here right now is just your

I’ve got up here right now is just your

I’ve got up here right now is just your typical hello world go web app example

typical hello world go web app example

typical hello world go web app example and what I’d like to do is show you how

and what I’d like to do is show you how

and what I’d like to do is show you how you can incorporate templates so first

you can incorporate templates so first

you can incorporate templates so first of all you know all the things that

of all you know all the things that

of all you know all the things that we’ve shown up to this point even in

we’ve shown up to this point even in

we’ve shown up to this point even in this tutorial what we’re going to show

this tutorial what we’re going to show

this tutorial what we’re going to show with templates is just gonna be super

with templates is just gonna be super

with templates is just gonna be super basic and you could do it all without

basic and you could do it all without

basic and you could do it all without templates the problem is when your code

templates the problem is when your code

templates the problem is when your code begins to get a little more complex and

begins to get a little more complex and

begins to get a little more complex and you start to incorporate things like

you start to incorporate things like

you start to incorporate things like JavaScript or even just even just when

JavaScript or even just even just when

JavaScript or even just even just when you start doing more complicated HTML

you start doing more complicated HTML

you start doing more complicated HTML systems and styling and all that I think

systems and styling and all that I think

systems and styling and all that I think it will be kind of problematic to keep

it will be kind of problematic to keep

it will be kind of problematic to keep using go in line with your code now some

using go in line with your code now some

using go in line with your code now some people might actually prefer that way

people might actually prefer that way

people might actually prefer that way and that’s totally fine I mean if you’re

and that’s totally fine I mean if you’re

and that’s totally fine I mean if you’re coming from something like PHP that

coming from something like PHP that

coming from something like PHP that might feel really natural to you and by

might feel really natural to you and by

might feel really natural to you and by all means go for it but for me I think

all means go for it but for me I think

all means go for it but for me I think it’s better I would rather it use

it’s better I would rather it use

it’s better I would rather it use templates but if you don’t want to use

templates but if you don’t want to use

templates but if you don’t want to use templates after this that’s probably

templates after this that’s probably

templates after this that’s probably fine you could get away with using that

fine you could get away with using that

fine you could get away with using that kind of Tildy multi-line stuff and

kind of Tildy multi-line stuff and

kind of Tildy multi-line stuff and probably be okay

probably be okay

probably be okay but anyways I’m gonna be showing you

but anyways I’m gonna be showing you

but anyways I’m gonna be showing you guys how to make use of the templates so

guys how to make use of the templates so

guys how to make use of the templates so with that let’s go ahead and get started

with that let’s go ahead and get started

with that let’s go ahead and get started so if you don’t have this code just go

so if you don’t have this code just go

so if you don’t have this code just go to this tutorial there’s a link in the

to this tutorial there’s a link in the

to this tutorial there’s a link in the description at least that leads to this

description at least that leads to this

description at least that leads to this go series and then you can just go to

go series and then you can just go to

go series and then you can just go to part 16 and grab the starting code if

part 16 and grab the starting code if

part 16 and grab the starting code if you want what I’m gonna do is I’m gonna

you want what I’m gonna do is I’m gonna

you want what I’m gonna do is I’m gonna go ahead and make a new page so we’re

go ahead and make a new page so we’re

go ahead and make a new page so we’re just gonna have HTTP dot handle func and

just gonna have HTTP dot handle func and

just gonna have HTTP dot handle func and then this is gonna lead to slash a GG

then this is gonna lead to slash a GG

then this is gonna lead to slash a GG for AG because it’ll be our news

for AG because it’ll be our news

for AG because it’ll be our news aggregator and I’m gonna have basically

aggregator and I’m gonna have basically

aggregator and I’m gonna have basically we’re just gonna make a news

we’re just gonna make a news

we’re just gonna make a news aggghhhhh handler we should probably

aggghhhhh handler we should probably

aggghhhhh handler we should probably there you go Handler and the new zag

there you go Handler and the new zag

there you go Handler and the new zag handler basically this is going to take

handler basically this is going to take

handler basically this is going to take your templates and or at least this is

your templates and or at least this is

your templates and or at least this is gonna be our page right which is going

gonna be our page right which is going

gonna be our page right which is going to use a template and when you use a

to use a template and when you use a

to use a template and when you use a template you’re going to execute a

template you’re going to execute a

template you’re going to execute a template and when you execute that

template and when you execute that

template and when you execute that template you can

template you can

template you can past the writer and then you pass the

past the writer and then you pass the

past the writer and then you pass the variable the single variable so if

variable the single variable so if

variable the single variable so if you’re not noticing a trend here and go

you’re not noticing a trend here and go

you’re not noticing a trend here and go you rarely get to pass multiple

you rarely get to pass multiple

you rarely get to pass multiple variables or values if you want to do

variables or values if you want to do

variables or values if you want to do that you need to struct so so that’s

that you need to struct so so that’s

that you need to struct so so that’s what we’re going to go ahead and do the

what we’re going to go ahead and do the

what we’re going to go ahead and do the lifeblood of go the struct so let’s go

lifeblood of go the struct so let’s go

lifeblood of go the struct so let’s go ahead and just do type and then we’ll

ahead and just do type and then we’ll

ahead and just do type and then we’ll call this new zag page and that will be

call this new zag page and that will be

call this new zag page and that will be a struct and for now we’re just gonna

a struct and for now we’re just gonna

a struct and for now we’re just gonna have two values we’re gonna have the

have two values we’re gonna have the

have two values we’re gonna have the title and that’ll be a string and then

title and that’ll be a string and then

title and that’ll be a string and then we’re gonna have news and that’ll be a

we’re gonna have news and that’ll be a

we’re gonna have news and that’ll be a string also before I forget let’s go

string also before I forget let’s go

string also before I forget let’s go ahead and make our import so we’re gonna

ahead and make our import so we’re gonna

ahead and make our import so we’re gonna bring in HTML slash template so that we

bring in HTML slash template so that we

bring in HTML slash template so that we can do the templating so now let’s go

can do the templating so now let’s go

can do the templating so now let’s go ahead and make our new zag handler so

ahead and make our new zag handler so

ahead and make our new zag handler so let me just and in fact actually I’m

let me just and in fact actually I’m

let me just and in fact actually I’m just gonna copy this so I have to write

just gonna copy this so I have to write

just gonna copy this so I have to write it all out and then I’m gonna copy and

it all out and then I’m gonna copy and

it all out and then I’m gonna copy and paste music handler nice so now what we

paste music handler nice so now what we

paste music handler nice so now what we want to do is just work on the code that

want to do is just work on the code that

want to do is just work on the code that we’re gonna need here so first of all

we’re gonna need here so first of all

we’re gonna need here so first of all we’re gonna use P to be our page so I’m

we’re gonna use P to be our page so I’m

we’re gonna use P to be our page so I’m just going to say P colon equals and

just going to say P colon equals and

just going to say P colon equals and that’s gonna be the new zag page and

that’s gonna be the new zag page and

that’s gonna be the new zag page and then in here we’re gonna just gonna hard

then in here we’re gonna just gonna hard

then in here we’re gonna just gonna hard code some values so we’re gonna say

code some values so we’re gonna say

code some values so we’re gonna say title and then the title can be

title and then the title can be

title and then the title can be literally anything I’m gonna call this

literally anything I’m gonna call this

literally anything I’m gonna call this the amazing news aggregator and then the

the amazing news aggregator and then the

the amazing news aggregator and then the news itself again we’re just gonna have

news itself again we’re just gonna have

news itself again we’re just gonna have you know some news for now so that’s

you know some news for now so that’s

you know some news for now so that’s gonna be our page and all its values now

gonna be our page and all its values now

gonna be our page and all its values now what we want to do is create the actual

what we want to do is create the actual

what we want to do is create the actual template itself so we’re gonna say

template itself so we’re gonna say

template itself so we’re gonna say template and then any error if it was

template and then any error if it was

template and then any error if it was passed we’re not gonna worry about

passed we’re not gonna worry about

passed we’re not gonna worry about errors right now eventually we’ll talk

errors right now eventually we’ll talk

errors right now eventually we’ll talk about errors and all that and the panic

about errors and all that and the panic

about errors and all that and the panic and recover all that stuff but for now

and recover all that stuff but for now

and recover all that stuff but for now we’re not going to do anything with it

we’re not going to do anything with it

we’re not going to do anything with it so we need to under case it because

so we need to under case it because

so we need to under case it because we’re not going to use it so so yeah so

we’re not going to use it so so yeah so

we’re not going to use it so so yeah so basically our template will be a

basically our template will be a

basically our template will be a template from what we imported above

template from what we imported above

template from what we imported above HTML slash template and then parse files

HTML slash template and then parse files

HTML slash template and then parse files parse files and the file we want to

parse files and the file we want to

parse files and the file we want to parse to be our template is going to be

parse to be our template is going to be

parse to be our template is going to be basic templating HTML so we’re gonna be

basic templating HTML so we’re gonna be

basic templating HTML so we’re gonna be making our template in there it’s gonna

making our template in there it’s gonna

making our template in there it’s gonna be

be

be this can be amazing HTML code so now

this can be amazing HTML code so now

this can be amazing HTML code so now that we’ve done that finally the last

that we’ve done that finally the last

that we’ve done that finally the last thing that we want to do is we need to

thing that we want to do is we need to

thing that we want to do is we need to actually execute this page so so what

actually execute this page so so what

actually execute this page so so what we’re going to do is we’re gonna say T

we’re going to do is we’re gonna say T

we’re going to do is we’re gonna say T for our template that we just did

for our template that we just did

for our template that we just did execute and then what we’re gonna

execute and then what we’re gonna

execute and then what we’re gonna execute is with our writer and then what

execute is with our writer and then what

execute is with our writer and then what we’re going to say is is we’re gonna

we’re going to say is is we’re gonna

we’re going to say is is we’re gonna pass any values so we’re passing P which

pass any values so we’re passing P which

pass any values so we’re passing P which is that news aggregator page with these

is that news aggregator page with these

is that news aggregator page with these values here so we can reference actually

values here so we can reference actually

values here so we can reference actually these values so with that we’re

these values so with that we’re

these values so with that we’re basically done we just need to actually

basically done we just need to actually

basically done we just need to actually write the basic templating HTML page so

write the basic templating HTML page so

write the basic templating HTML page so I’ve already got one created or at least

I’ve already got one created or at least

I’ve already got one created or at least I created the file but we’re gonna need

I created the file but we’re gonna need

I created the file but we’re gonna need to populate it so if you don’t have this

to populate it so if you don’t have this

to populate it so if you don’t have this already go ahead and make one real quick

already go ahead and make one real quick

already go ahead and make one real quick open it up and you’re ready to go so

open it up and you’re ready to go so

open it up and you’re ready to go so we’re gonna make first the basically the

we’re gonna make first the basically the

we’re gonna make first the basically the title so we’re gonna put that in header

title so we’re gonna put that in header

title so we’re gonna put that in header one tags a nice big tags and then to

one tags a nice big tags and then to

one tags a nice big tags and then to specify any sort of structures values

specify any sort of structures values

specify any sort of structures values whatever inside or basically inside your

whatever inside or basically inside your

whatever inside or basically inside your templates but go structures sort of you

templates but go structures sort of you

templates but go structures sort of you you need the double curly braces okay

you need the double curly braces okay

you need the double curly braces okay and so what we’re gonna do inside these

and so what we’re gonna do inside these

and so what we’re gonna do inside these double curly braces is specify we wanna

double curly braces is specify we wanna

double curly braces is specify we wanna we want just the title so we’re gonna

we want just the title so we’re gonna

we want just the title so we’re gonna use a dot to note basically this is a

use a dot to note basically this is a

use a dot to note basically this is a variable here and we’re just going to

variable here and we’re just going to

variable here and we’re just going to say title title and if title had values

say title title and if title had values

say title title and if title had values you could do title dot i don’t really

you could do title dot i don’t really

you could do title dot i don’t really like impose titles like a list of titles

like impose titles like a list of titles

like impose titles like a list of titles like we might have it could be titles

like we might have it could be titles

like we might have it could be titles dot title or something like that or

dot title or something like that or

dot title or something like that or probably title with this anyway but we

probably title with this anyway but we

probably title with this anyway but we just want titles that’s it so we just

just want titles that’s it so we just

just want titles that’s it so we just use that first dot and then what we’re

use that first dot and then what we’re

use that first dot and then what we’re gonna do is after our titles we would

gonna do is after our titles we would

gonna do is after our titles we would have I don’t know some news so we’ll

have I don’t know some news so we’ll

have I don’t know some news so we’ll have some paragraph tags and then again

have some paragraph tags and then again

have some paragraph tags and then again here we would just do dot News and we

here we would just do dot News and we

here we would just do dot News and we can save that so once we have that we

can save that so once we have that we

can save that so once we have that we are ready to actually run this

are ready to actually run this

are ready to actually run this application so I’m gonna go ahead and

application so I’m gonna go ahead and

application so I’m gonna go ahead and just go run go touch so go run go to see

just go run go touch so go run go to see

just go run go touch so go run go to see if we have any errors we do we use

if we have any errors we do we use

if we have any errors we do we use probably a single quote at some point in

probably a single quote at some point in

probably a single quote at some point in line 16 I’m not seeing it let’s see 16

line 16 I’m not seeing it let’s see 16

line 16 I’m not seeing it let’s see 16 here okay yep

here okay yep

here okay yep I was looking at the wrong line so

I was looking at the wrong line so

I was looking at the wrong line so someone asked and I think it’s a great

someone asked and I think it’s a great

someone asked and I think it’s a great question why can’t we use single quotes

question why can’t we use single quotes

question why can’t we use single quotes and go have I just not maybe I’ve not

and go have I just not maybe I’ve not

and go have I just not maybe I’ve not reached the point where a single quote

reached the point where a single quote

reached the point where a single quote is actually like a really meaningful

is actually like a really meaningful

is actually like a really meaningful thing and go but if someone has that

thing and go but if someone has that

thing and go but if someone has that answer feel free to let us know cuz I

answer feel free to let us know cuz I

answer feel free to let us know cuz I still haven’t come across any reason why

still haven’t come across any reason why

still haven’t come across any reason why I can’t use a single quote T dot execute

I can’t use a single quote T dot execute

I can’t use a single quote T dot execute what did I do wrong there did we use a

what did I do wrong there did we use a

what did I do wrong there did we use a underscore we did so that would never

underscore we did so that would never

underscore we did so that would never work right needs to be exported so

work right needs to be exported so

work right needs to be exported so capital that capitalized that e try

capital that capitalized that e try

capital that capitalized that e try again third time’s a charm oh yeah here

again third time’s a charm oh yeah here

again third time’s a charm oh yeah here we go I’ll allow it

we go I’ll allow it

we go I’ll allow it let’s do it wait for it okay so what we

let’s do it wait for it okay so what we

let’s do it wait for it okay so what we can do now is we can come to our home

can do now is we can come to our home

can do now is we can come to our home page I’m just going to do slash tag and

page I’m just going to do slash tag and

page I’m just going to do slash tag and [Music]

[Music]

[Music] interestingly I am empty-handed what did

interestingly I am empty-handed what did

interestingly I am empty-handed what did we do wrong basic templating that HTML

we do wrong basic templating that HTML

we do wrong basic templating that HTML for some reason I don’t have anything in

for some reason I don’t have anything in

for some reason I don’t have anything in egg title news up okay sorry

egg title news up okay sorry

egg title news up okay sorry so man typos galore anyway I simply

so man typos galore anyway I simply

so man typos galore anyway I simply wanted to show an example with titles

wanted to show an example with titles

wanted to show an example with titles and then I screwed myself and I think

and then I screwed myself and I think

and then I screwed myself and I think news stayed news so here we can actually

news stayed news so here we can actually

news stayed news so here we can actually just modify the HTML template we don’t

just modify the HTML template we don’t

just modify the HTML template we don’t have to restart anything hopefully that

have to restart anything hopefully that

have to restart anything hopefully that fixes it sure enough it does okay so so

fixes it sure enough it does okay so so

fixes it sure enough it does okay so so yeah if you’re missing that variable it

yeah if you’re missing that variable it

yeah if you’re missing that variable it won’t error out on you it just I mean it

won’t error out on you it just I mean it

won’t error out on you it just I mean it errors somewhere it’s just very

errors somewhere it’s just very

errors somewhere it’s just very secretive to you and that’s why you need

secretive to you and that’s why you need

secretive to you and that’s why you need to eventually have probably the cover of

to eventually have probably the cover of

to eventually have probably the cover of the panics and all that because errors

the panics and all that because errors

the panics and all that because errors and go are only occur really if you ask

and go are only occur really if you ask

and go are only occur really if you ask them to occur otherwise they’re kind of

them to occur otherwise they’re kind of

them to occur otherwise they’re kind of hidden and and they don’t really bug you

hidden and and they don’t really bug you

hidden and and they don’t really bug you which can be kind of problematic when

which can be kind of problematic when

which can be kind of problematic when you hit issues like this so I’m guessing

you hit issues like this so I’m guessing

you hit issues like this so I’m guessing if we had not done this like I’m just

if we had not done this like I’m just

if we had not done this like I’m just curious at this point I’ve actually not

curious at this point I’ve actually not

curious at this point I’ve actually not tested this I’m just going to go ahead

tested this I’m just going to go ahead

tested this I’m just going to go ahead and throw air there and then

and throw air there and then

and throw air there and then format diet wine air it’ll probably nil

format diet wine air it’ll probably nil

format diet wine air it’ll probably nil i I’m not even sure if it prints out now

i I’m not even sure if it prints out now

i I’m not even sure if it prints out now you can also just say if air not equals

you can also just say if air not equals

you can also just say if air not equals nil something like that print that out

nil something like that print that out

nil something like that print that out let me go ahead and save that real quick

let me go ahead and save that real quick

let me go ahead and save that real quick I’m just curious to this point this

I’m just curious to this point this

I’m just curious to this point this tutorial is over you’re probably going

tutorial is over you’re probably going

tutorial is over you’re probably going to learn anything new besides answering

to learn anything new besides answering

to learn anything new besides answering this question I’ll allow it but I’m just

this question I’ll allow it but I’m just

this question I’ll allow it but I’m just curious so okay so it does at least

curious so okay so it does at least

curious so okay so it does at least print nil and then if we go and change

print nil and then if we go and change

print nil and then if we go and change this to be like titles save that just

this to be like titles save that just

this to be like titles save that just for the record I mean you should be

for the record I mean you should be

for the record I mean you should be seeing this on your screen too but yeah

seeing this on your screen too but yeah

seeing this on your screen too but yeah let me just refresh it again oh so no

let me just refresh it again oh so no

let me just refresh it again oh so no that’s not giving us the error hmm I

that’s not giving us the error hmm I

that’s not giving us the error hmm I wonder where the error would happen if

wonder where the error would happen if

wonder where the error would happen if it would be maybe at this stage no I

it would be maybe at this stage no I

it would be maybe at this stage no I wouldn’t have it in this stage you would

wouldn’t have it in this stage you would

wouldn’t have it in this stage you would think it would happen right here at this

think it would happen right here at this

think it would happen right here at this stage maybe T dot execute returns an

stage maybe T dot execute returns an

stage maybe T dot execute returns an error if it has it I’m just curious like

error if it has it I’m just curious like

error if it has it I’m just curious like I said tutorials over if you want to

I said tutorials over if you want to

I said tutorials over if you want to leave feel free templates what was that

leave feel free templates what was that

leave feel free templates what was that HTML template going then I’ll just

HTML template going then I’ll just

HTML template going then I’ll just search execute of course we could oh

search execute of course we could oh

search execute of course we could oh yeah so T execute does return an error

yeah so T execute does return an error

yeah so T execute does return an error that was quick so one thing we could do

format dot print line

format dot print line [Music]

[Music]

[Music] so rather than this error and I’ll just

so rather than this error and I’ll just

so rather than this error and I’ll just comment out that error let’s see if we

comment out that error let’s see if we

comment out that error let’s see if we get an error this time don’t you just

get an error this time don’t you just

get an error this time don’t you just love go I wish you could declare stuff

love go I wish you could declare stuff

love go I wish you could declare stuff and not use it that drives me nuts

and not use it that drives me nuts

and not use it that drives me nuts because I’m just trying to debug that I

because I’m just trying to debug that I

because I’m just trying to debug that I keep running into it because of that I

keep running into it because of that I

keep running into it because of that I mean I’m definitely notorious for for

mean I’m definitely notorious for for

mean I’m definitely notorious for for declaring stuff and not using it but

declaring stuff and not using it but

declaring stuff and not using it but that’s annoying

that’s annoying

that’s annoying so nil no let’s go ahead and cause an

so nil no let’s go ahead and cause an

so nil no let’s go ahead and cause an error see if this won’t do it for us Oh

error see if this won’t do it for us Oh

error see if this won’t do it for us Oh reload the page there we go nice so yeah

reload the page there we go nice so yeah

reload the page there we go nice so yeah so there’s how you couldn’t could have

so there’s how you couldn’t could have

so there’s how you couldn’t could have acquired the error

acquired the error

acquired the error but we were letting it pass silently

but we were letting it pass silently

but we were letting it pass silently because it’s go and that’s what that’s

because it’s go and that’s what that’s

because it’s go and that’s what that’s what errors do and go you have to you

what errors do and go you have to you

what errors do and go you have to you have to specifically ask for them to

have to specifically ask for them to

have to specifically ask for them to rear their heads anyway

rear their heads anyway

rear their heads anyway hi I’m interesting and that’s why most

hi I’m interesting and that’s why most

hi I’m interesting and that’s why most go programs they don’t look like this a

go programs they don’t look like this a

go programs they don’t look like this a lot of times a little say like if air

lot of times a little say like if air

lot of times a little say like if air equals no or if air yeah either Eagles

equals no or if air yeah either Eagles

equals no or if air yeah either Eagles no or if error does not equal meal what

no or if error does not equal meal what

no or if error does not equal meal what are we gonna do let’s actually print it

are we gonna do let’s actually print it

are we gonna do let’s actually print it out and all that

out and all that

out and all that not if air equals no anyways oh okay

not if air equals no anyways oh okay

not if air equals no anyways oh okay cool a little bonus for anybody who’s

cool a little bonus for anybody who’s

cool a little bonus for anybody who’s happened to stick around that’s it for

happened to stick around that’s it for

happened to stick around that’s it for now what we’re going to be doing in the

now what we’re going to be doing in the

now what we’re going to be doing in the next tutorial is applying these

next tutorial is applying these

next tutorial is applying these templates to our web app and actually

templates to our web app and actually

templates to our web app and actually doing something that involves a little

doing something that involves a little

doing something that involves a little more HTML because obviously this was

more HTML because obviously this was

more HTML because obviously this was something we could have very easily

something we could have very easily

something we could have very easily maybe even easy you’re more easily done

maybe even easy you’re more easily done

maybe even easy you’re more easily done I don’t know anyway could have been

I don’t know anyway could have been

I don’t know anyway could have been easier maybe even just doing it in line

easier maybe even just doing it in line

easier maybe even just doing it in line with go so anyways let’s go ahead and

with go so anyways let’s go ahead and

with go so anyways let’s go ahead and actually apply this to something a

actually apply this to something a

actually apply this to something a little more complicated but not too

little more complicated but not too

little more complicated but not too complicated

complicated

complicated with our news aggregator web app if you

with our news aggregator web app if you

with our news aggregator web app if you guys have questions comments concerns

guys have questions comments concerns

guys have questions comments concerns whatever feel free to leave them below

whatever feel free to leave them below

whatever feel free to leave them below otherwise I will see you in the next

otherwise I will see you in the next

otherwise I will see you in the next tutorial

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *