gomodest-template
A modest template to build dynamic web apps in Go, HTML and sprinkles and spots of javascript.
Why ?
- Build dynamic websites using the tools you already know(Go, HTML, CSS, Vanilla Javascript) for the most part.
- Use bulma components to speed up prototyping a responsive and good-looking UI.
- Use turbo & stimulusjs for most of the interactivity.
- For really complex interactivity use Svelte for a single div in a few spots.
- Lightweight and productive. Fast development cycle.
- Easy to start, easy to maintain.
For a more complete implementation using this technique please see gomodest-starter.
Usage
- Use as a github template
git clone https://github.com/
and/ cd /path/to/your/gomodest-template
make watch
(starts hot-reload for go, html and javascript changes)- open localhost:3000.
or
brew install gh
gh repo create myapp --template adnaan/gomodest-template
cd myapp
git pull origin main
make install
goreplace gomodest-template -r myapp
make watch
TOC
- Folder Structure
- Views using html templates
- Interactivity using Javascript
- Styling and Images
- Samples
- Dependencies
Table of contents generated with markdown-toc
Folder Structure
-
templates/
- layouts/
- partials/
- list of view files
-
assets/
-
images/
-
src/
- components/
- controllers/
- index.js
- styles.scss
-
-
templates
is the root directory where all html/templates assets are found. -
layouts
contains the layout files. Layout is a container forpartials
andview files
-
partials
contains the partial files. Partial is a reusable html template which can be used in one of two ways:-
Included in a
layout
file:{{include "partials/header"}}
-
Included in a
view
file:{{template "main" .}}
. When used in a view file, a partial must be enclosed in adefine
tag:{{define "main"}} Hello {{.hello}} {{end}}
-
-
view
files are put in the root of thetemplates
directory. They are contained within alayout
must be enclosed in adefine content
tag:{{define "content"}} App's {{.dashboard}} {{end}}
View
is rendered within alayout
:indexLayout, err := rl.New( rl.Layout("index"), rl.DisableCache(true), rl.DefaultHandler(func(w http.ResponseWriter, r *http.Request) (rl.M, error) { return rl.M{ "app_name": "gomdest-template", }, nil })) ... r.Get("/", indexLayout.Handle("home", func(w http.ResponseWriter, r *http.Request) (rl.M, error) { return rl.M{ "hello": "world", }, nil }))
Here the
view
:home
is rendered within theindex
layout.
Please see the templates
directory.
-
assets
directory contains the public asset pipeline for the project.styles.scss
is a customscss
file for [bulma][https://bulma.io] as documented here.index.js
is the entrypoint for loadingstimulusjs
controllers sourced from this [example](https://github.com/hotwired/stimulus-starter.controllers
contains stimulusjs controllers.components
contains single file svelte components.
Views using html templates
There are three kinds of html/template
files in this project.
layout
: defines the base structure of a web page.partial
: reusable snippets of html. It can be of two types:layout partials
&view partials
.view
: the main container for the web page logic contained within alayout
. It must be enclosed in adefine content
template definition. It can use view partials.
Step 1: Add a layout partial
Create header.html
file in templates/partials
.
<meta charset="UTF-8">
<meta name="description" content="A modest way to build golang web apps">
<meta name="viewport"content="width=device-width, initial-scale=1.0, maximum-scale=5.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
...
Step 2: Add a layout
Create index.html
file in templates/layouts
and use the above partial.