-
Notifications
You must be signed in to change notification settings - Fork 1
e Workshop 01: Load a template from module.
In this workshop you will learn how to build a template and use it as a module, within another project.
- Go +v1.7 with
$GOPATH
set. - GopherSauce. Learn here.
- ~ 30 minutes time.
-
To get started, open your terminal of choice, then run the following command (this is to go to the root of your $GOPATH):
cd $GOPATH/src/
-
Run the next command to make two new directories, one will be used as a package, and the other, as a web server.
mkdir gospkg && mkdir gostest
-
Change the working directory to
gospkg
to begin developing the module.
-
Run the following command to setup a new project within directory :
gos --make
-
Open the file
gos.gxml
within your project's directory, with your favorite code editor. -
Update the tag deploy from
<deploy>webapp</deploy>
to<deploy>package</deploy>
. -
Update the tag package from
<package>if-package-is-library</package>
to<package>gospkg</package>
(With your gospkg
directory as the working directory.)
-
Add a new template file to your
tmpl
(templates) folder, this file will be namedlogo.tmpl
. -
Open
tmpl/img.tmpl
with your favorite text editor. Add the following contents to it and save:<img src="{{.Src}}" />
-
Open the file
gos.gxml
within your project's directory, with your favorite code editor. -
Within your
gos.gxml
file, add the following struct tag within your existing<header/>
tag. This will declare the struct that will be used with your template. The field Src will be used to set thesrc
attribute of your template.<struct name="Image"> Src string </struct>
-
Within your
gos.gxml
file, add the following function tag within your existing<methods/>
tag. The function will be used to generate a new struct literal within your templates, so that we may update the image source used with template pipelines. The tag should be as following :<func name="MakeImage" var="src string" return="Image"> return Image{Src : src } </func>
-
Let's make sure to write a unit test for this function. Add a new file named
gos_test.go
to your project with the following content :package gospkg import "testing" func TestMakeImage(t *testing.T) { // Desired image source value src := "test" // load package to test function with pkg := PKG{} // invoke function as struct method, // pass expected parameter as separate // string img := pkg.MakeImage("test") if img.Src != src { t.Errorf("Image source was incorrect, got: %s, want: %s.", img.Src, src) } }
-
-
Within your
gos.gxml
file, add the following template tag within your existing<templates/>
tag. The tag should be as following :<template name="Img" tmpl="img" struct="Image" />
** The name attribute of this tag will be the name of the module function.
-
Run the following command to export your package. This will enable use of modules with templates.
gos --export
-
Run the following command to switch to the web server project. We will use the module we just created with an active web page.
cd ../gostest
-
Create a new Gopher Sauce project with the following command :
gos --make
-
Open the file
gos.gxml
within your project's directory, with your favorite code editor. -
Import the module we just created with the following import tag, add it after your existing
</error>
tag. The path is relative to your$GOPATH
<import src="gospkg/gos.gxml"/>
-
Create a new file within your project's
web
directory. This file will be namedindex.tmpl
. -
Open file
web/index.tmpl
with your favorite text editor, and add the following line to import this new module.{{ $pkg := gospkg }}
-
Add the following line to
web/index.tmpl
to :-
Allocate a new struct with the specified source
-
Assign struct literal returned from pipeline to variable
$img
.{{ $img := $pkg.MakeImage "https://placehold.it/500x500"}}
-
-
Add the following line to render an HTML template with the previously declared variable
$img
.{{ $pkg.Img $img }}
-
Run the following command to launch the server :
gos --run
-
Open your favorite browser to localhost:8080 to view the result.