Skip to content

Template modules.

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

Package modules

As of Gopher Sauce version 0.6.0, you may import packages to your templates as modules. The module approach was implemented to give developers the possibility of isolating (encapsulating) code and choosing which packages are loaded (to manage performance, avoid declaration conflicts).

Package modules are generated from Gopher Sauce projects with deploy type set to package. The package name set will be used to load your module within a template. Insert a new import tag within your project to add a package with template modules. The newly inserted tag must point to a GXML source file, Gopher Sauce will handle the rest.

Package spec

Building

You must export a Gopher Sauce project with type package. This is to export the code from GXML to Go code. Use command gos --export to export a project, make sure your terminal's working directory is your project's.

Module spec

Package level

Gopher Sauce does not support package modules with endpoint declarations or files within your package's web root.

Global level

Each module is loaded through a template pipeline. The pipeline will be named after the value set within your package's GXML tag : <package>${MODULE NAME}</package>. This pipeline returns a struct literal from your module's package, this struct has your package's templates and declared pipelines as struct methods.

Exporting a function or template works the same way as in Go, prefix the name of your method or template with a capital letter.

Template level

To load a module, invoke the pipeline corresponding to the package name. Having a method name, local to your project, similar to the name of an imported package, will cause an error.

e-Workshop

Try out the Package module workshops to see modules in action :

Package Guidelines

  • Your exported package methods must return a variable.
  • Your package's GXML must have a package name set.
  • Your templates' name should be capitalized to export it.
  • Your package should include a readme with basic examples of template module.
  • Your package should include unit tests.