Skip to content

e Workshop 01: Load a template from module.

Cheikh Seck edited this page Nov 2, 2018 · 3 revisions

e-Workshop 01 : Load a template from module.

Overview

In this workshop you will learn how to build a template and use it as a module, within another project.

Checklist

  • Go +v1.7 with $GOPATH set.
  • GopherSauce. Learn here.
  • ~ 30 minutes time.

Setup

  1. 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/
    
  2. 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
    
  3. Change the working directory to gospkg to begin developing the module.

Start the project

  1. Run the following command to setup a new project within directory :

     gos --make
    
  2. Open the file gos.gxml within your project's directory, with your favorite code editor.

  3. Update the tag deploy from <deploy>webapp</deploy> to <deploy>package</deploy>.

  4. Update the tag package from <package>if-package-is-library</package> to <package>gospkg</package>

Create a module template

(With your gospkg directory as the working directory.)

  1. Add a new template file to your tmpl (templates) folder, this file will be named logo.tmpl.

  2. Open tmpl/img.tmpl with your favorite text editor. Add the following contents to it and save:

     <img src="{{.Src}}" />
    
  3. Open the file gos.gxml within your project's directory, with your favorite code editor.

  4. 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 the src attribute of your template.

     <struct name="Image">
      	Src string
     </struct> 
    
  5. 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>
    
    1. 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)
           }
       }
      
  6. 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.

Export package

  1. Run the following command to export your package. This will enable use of modules with templates.

     gos --export
    

Switch to web server.

  1. 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 
    
  2. Create a new Gopher Sauce project with the following command :

     gos --make
    

Import module to project.

  1. Open the file gos.gxml within your project's directory, with your favorite code editor.

  2. 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"/>
    

Use template module

  1. Create a new file within your project's web directory. This file will be named index.tmpl.

  2. Open file web/index.tmpl with your favorite text editor, and add the following line to import this new module.

     {{ $pkg := gospkg }}
    
  3. Add the following line to web/index.tmpl to :

    1. Allocate a new struct with the specified source

    2. Assign struct literal returned from pipeline to variable $img.

      {{ $img := $pkg.MakeImage "https://placehold.it/500x500"}}
      
  4. Add the following line to render an HTML template with the previously declared variable $img.

     {{ $pkg.Img $img }}
    

Run and test

  1. Run the following command to launch the server :

     gos --run
    
  2. Open your favorite browser to localhost:8080 to view the result.