Press "Enter" to skip to content

HTML templates – Go Lang Practical Programming Tutorial p.16


what is going on everybody welcome to
part 16 of the go language tutorial
series in this part what we’re gonna be
talking about is templating with gos
HTML slash templating package so what
I’ve got up here right now is just your
typical hello world go web app example
and what I’d like to do is show you how
you can incorporate templates so first
of all you know all the things that
we’ve shown up to this point even in
this tutorial what we’re going to show
with templates is just gonna be super
basic and you could do it all without
templates the problem is when your code
begins to get a little more complex and
you start to incorporate things like
JavaScript or even just even just when
you start doing more complicated HTML
systems and styling and all that I think
it will be kind of problematic to keep
using go in line with your code now some
people might actually prefer that way
and that’s totally fine I mean if you’re
coming from something like PHP that
might feel really natural to you and by
all means go for it but for me I think
it’s better I would rather it use
templates but if you don’t want to use
templates after this that’s probably
fine you could get away with using that
kind of Tildy multi-line stuff and
probably be okay
but anyways I’m gonna be showing you
guys how to make use of the templates so
with that let’s go ahead and get started
so if you don’t have this code just go
to this tutorial there’s a link in the
description at least that leads to this
go series and then you can just go to
part 16 and grab the starting code if
you want what I’m gonna do is I’m gonna
go ahead and make a new page so we’re
just gonna have HTTP dot handle func and
then this is gonna lead to slash a GG
for AG because it’ll be our news
aggregator and I’m gonna have basically
we’re just gonna make a news
aggghhhhh handler we should probably
there you go Handler and the new zag
handler basically this is going to take
your templates and or at least this is
gonna be our page right which is going
to use a template and when you use a
template you’re going to execute a
template and when you execute that
template you can
past the writer and then you pass the
variable the single variable so if
you’re not noticing a trend here and go
you rarely get to pass multiple
variables or values if you want to do
that you need to struct so so that’s
what we’re going to go ahead and do the
lifeblood of go the struct so let’s go
ahead and just do type and then we’ll
call this new zag page and that will be
a struct and for now we’re just gonna
have two values we’re gonna have the
title and that’ll be a string and then
we’re gonna have news and that’ll be a
string also before I forget let’s go
ahead and make our import so we’re gonna
bring in HTML slash template so that we
can do the templating so now let’s go
ahead and make our new zag handler so
let me just and in fact actually I’m
just gonna copy this so I have to write
it all out and then I’m gonna copy and
paste music handler nice so now what we
want to do is just work on the code that
we’re gonna need here so first of all
we’re gonna use P to be our page so I’m
just going to say P colon equals and
that’s gonna be the new zag page and
then in here we’re gonna just gonna hard
code some values so we’re gonna say
title and then the title can be
literally anything I’m gonna call this
the amazing news aggregator and then the
news itself again we’re just gonna have
you know some news for now so that’s
gonna be our page and all its values now
what we want to do is create the actual
template itself so we’re gonna say
template and then any error if it was
passed we’re not gonna worry about
errors right now eventually we’ll talk
about errors and all that and the panic
and recover all that stuff but for now
we’re not going to do anything with it
so we need to under case it because
we’re not going to use it so so yeah so
basically our template will be a
template from what we imported above
HTML slash template and then parse files
parse files and the file we want to
parse to be our template is going to be
basic templating HTML so we’re gonna be
making our template in there it’s gonna
be
this can be amazing HTML code so now
that we’ve done that finally the last
thing that we want to do is we need to
actually execute this page so so what
we’re going to do is we’re gonna say T
for our template that we just did
execute and then what we’re gonna
execute is with our writer and then what
we’re going to say is is we’re gonna
pass any values so we’re passing P which
is that news aggregator page with these
values here so we can reference actually
these values so with that we’re
basically done we just need to actually
write the basic templating HTML page so
I’ve already got one created or at least
I created the file but we’re gonna need
to populate it so if you don’t have this
already go ahead and make one real quick
open it up and you’re ready to go so
we’re gonna make first the basically the
title so we’re gonna put that in header
one tags a nice big tags and then to
specify any sort of structures values
whatever inside or basically inside your
templates but go structures sort of you
you need the double curly braces okay
and so what we’re gonna do inside these
double curly braces is specify we wanna
we want just the title so we’re gonna
use a dot to note basically this is a
variable here and we’re just going to
say title title and if title had values
you could do title dot i don’t really
like impose titles like a list of titles
like we might have it could be titles
dot title or something like that or
probably title with this anyway but we
just want titles that’s it so we just
use that first dot and then what we’re
gonna do is after our titles we would
have I don’t know some news so we’ll
have some paragraph tags and then again
here we would just do dot News and we
can save that so once we have that we
are ready to actually run this
application so I’m gonna go ahead and
just go run go touch so go run go to see
if we have any errors we do we use
probably a single quote at some point in
line 16 I’m not seeing it let’s see 16
here okay yep
I was looking at the wrong line so
someone asked and I think it’s a great
question why can’t we use single quotes
and go have I just not maybe I’ve not
reached the point where a single quote
is actually like a really meaningful
thing and go but if someone has that
answer feel free to let us know cuz I
still haven’t come across any reason why
I can’t use a single quote T dot execute
what did I do wrong there did we use a
underscore we did so that would never
work right needs to be exported so
capital that capitalized that e try
again third time’s a charm oh yeah here
we go I’ll allow it
let’s do it wait for it okay so what we
can do now is we can come to our home
page I’m just going to do slash tag and
[Music]
interestingly I am empty-handed what did
we do wrong basic templating that HTML
for some reason I don’t have anything in
egg title news up okay sorry
so man typos galore anyway I simply
wanted to show an example with titles
and then I screwed myself and I think
news stayed news so here we can actually
just modify the HTML template we don’t
have to restart anything hopefully that
fixes it sure enough it does okay so so
yeah if you’re missing that variable it
won’t error out on you it just I mean it
errors somewhere it’s just very
secretive to you and that’s why you need
to eventually have probably the cover of
the panics and all that because errors
and go are only occur really if you ask
them to occur otherwise they’re kind of
hidden and and they don’t really bug you
which can be kind of problematic when
you hit issues like this so I’m guessing
if we had not done this like I’m just
curious at this point I’ve actually not
tested this I’m just going to go ahead
and throw air there and then
format diet wine air it’ll probably nil
i I’m not even sure if it prints out now
you can also just say if air not equals
nil something like that print that out
let me go ahead and save that real quick
I’m just curious to this point this
tutorial is over you’re probably going
to learn anything new besides answering
this question I’ll allow it but I’m just
curious so okay so it does at least
print nil and then if we go and change
this to be like titles save that just
for the record I mean you should be
seeing this on your screen too but yeah
let me just refresh it again oh so no
that’s not giving us the error hmm I
wonder where the error would happen if
it would be maybe at this stage no I
wouldn’t have it in this stage you would
think it would happen right here at this
stage maybe T dot execute returns an
error if it has it I’m just curious like
I said tutorials over if you want to
leave feel free templates what was that
HTML template going then I’ll just
search execute of course we could oh
yeah so T execute does return an error
that was quick so one thing we could do
format dot print line
[Music]
so rather than this error and I’ll just
comment out that error let’s see if we
get an error this time don’t you just
love go I wish you could declare stuff
and not use it that drives me nuts
because I’m just trying to debug that I
keep running into it because of that I
mean I’m definitely notorious for for
declaring stuff and not using it but
that’s annoying
so nil no let’s go ahead and cause an
error see if this won’t do it for us Oh
reload the page there we go nice so yeah
so there’s how you couldn’t could have
acquired the error
but we were letting it pass silently
because it’s go and that’s what that’s
what errors do and go you have to you
have to specifically ask for them to
rear their heads anyway
hi I’m interesting and that’s why most
go programs they don’t look like this a
lot of times a little say like if air
equals no or if air yeah either Eagles
no or if error does not equal meal what
are we gonna do let’s actually print it
out and all that
not if air equals no anyways oh okay
cool a little bonus for anybody who’s
happened to stick around that’s it for
now what we’re going to be doing in the
next tutorial is applying these
templates to our web app and actually
doing something that involves a little
more HTML because obviously this was
something we could have very easily
maybe even easy you’re more easily done
I don’t know anyway could have been
easier maybe even just doing it in line
with go so anyways let’s go ahead and
actually apply this to something a
little more complicated but not too
complicated
with our news aggregator web app if you
guys have questions comments concerns
whatever feel free to leave them below
otherwise I will see you in the next
tutorial
Please follow and like us:

Be First to Comment

Leave a Reply