thtml

command module
v0.0.0-...-7b1ff6f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2019 License: MIT Imports: 13 Imported by: 0

README

Template HTML

GoDoc GitHub release Build Status MIT license Go Report Card

Static site generator based on Go's text/template with development web server integrated.

Install latest version using Go

go install github.com/leonelquinteros/thtml

Quick Start

$ thtml -h
Usage of thtml:
  -build
    	Build the assets from the -public directory to the -output directory by parsing the -templates directory.
  -exts string
    	Provides a comma separated filename extensions list to support when parsing templates. (default ".html")
  -init
    	Creates a new project structure into the current directory.
  -listen string
    	Run the dev server listening on the provided host:port. (default "localhost:5500")
  -minify
    	Minify the build output. (default true)
  -output string
    	Sets the path for the build output. (default "build")
  -public string
    	Sets the path for the web root. (default "public")
  -run
    	Run a dev web server serving the public directory.
  -templates string
    	Sets the path for the template files. (default "templates")
  -version
    	Prints version number.

Go template syntax and docs: https://golang.org/pkg/text/template

Creating static websites

1. Prepare the environment

After installing the thtml tool, we'll create a new project directory for our website development files. thtml will create a scaffolded website structure, based on a dummy Twitter Bootstrap template, were we can start working on.

$ mkdir mywebsite
$ cd mywebsite
$ thtml -init
2019/10/16 22:43:23 => Creating directory structure...
2019/10/16 22:43:23 public/css
2019/10/16 22:43:23 public/js
2019/10/16 22:43:23 public/img
2019/10/16 22:43:23 templates/layouts
2019/10/16 22:43:23 templates/components
2019/10/16 22:43:23 => Fetching files...
2019/10/16 22:43:23 public/css/bootstrap.min.css
2019/10/16 22:43:24 public/css/bootstrap.min.css.map
2019/10/16 22:43:25 public/img/bg-banner.jpg
2019/10/16 22:43:25 public/js/bootstrap.min.js
2019/10/16 22:43:25 public/js/bootstrap.min.js.map
2019/10/16 22:43:25 public/js/jquery-3.2.1.slim.min.js
2019/10/16 22:43:26 public/js/popper.min.js
2019/10/16 22:43:26 public/index.html
2019/10/16 22:43:26 public/page.html
2019/10/16 22:43:27 templates/components/footer.html
2019/10/16 22:43:27 templates/components/head.html
2019/10/16 22:43:27 templates/components/nav.html
2019/10/16 22:43:27 templates/layouts/default.html
2019/10/16 22:43:27 templates/layouts/two-columns.html
Finished.
$

Inside the project directory 2 new directories are created.

templates will contain all our template files that will be reused from our website pages.

public will contain the website pages and structure.

2. Work with the layout

We'll start by putting together a basic website layout based on Bootstrap that will be used by all the pages of our example website.

The newly created file templates/layouts/default.html has the following content:

{{ template "components/head.html" }}

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <small>
                Edit <code>templates/layouts/default.html</code> to change this layout
            </small>
        </div>
    </div>
    {{ template "components/nav.html" }}

    <div class="row">
        <div class="col-md-12">
            {{ block "view-content" . }}{{ end }}
        </div>
    </div>
</div>

{{ template "components/footer.html" }}

The entire file is a basic Bootstrap template, the only interesting line here is the one that says {{ block "view-content" . }}{{ end }}, which defines a content block that will be extended by our pages.

3. Create the first page

Now lets see the file at public/index.html with the following content:

{{ template "layouts/default.html" }}

{{ define "layout-title" }}Home title{{ end }}

{{ define "view-content" }}

    <h1>Home page</h1>

    <p>
        <img class="img-fluid" src="/img/bg-banner.jpg" alt="Image" />
        <br />
        
        <small>
            Edit <code>public/index.html</code> to change this page
        </small>
    </p>

    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </p>

    <p>
        <a href="/page.html" class="btn btn-primary">I'm a button!</a>
    </p>

{{ end }}

Here we see 2 new interesting lines. First one is {{ template "layouts/default.html" }} that loads the template located in templates/layouts/default.html by referencing it using a relative path from the templates directory.

Then there is the {{ define "view-content" }} that defines a block of code with the name of view-content that's the name of the content block we defined in our layout file.

4. Run development server

While we create our pages, we need to quickly see what's happening and how they look. For that purpose, we'll use the run mode of the thtml tool to run a local development web server to serve our website before being compiled to a static form:

thtml -run

With all the default options, the tool will use the templates and public directories properly. After running the command, we can open http://localhost:5500 in our browser to see our home page compiled and running.

After making any changes to the page or the layout, we can refresh the browser to see these changes while the web server keeps running. Try it!

5. Continue working

Go ahead and create more pages. You can also take the menu to a different file into a different directory (under the templates directory). Feel free to get ideas from the _example website.

6. Build your static website

After you finish developing and your website is ready to go live, you can compile it by running:

thtml -build

This will create a static version of your website into the build directory by default, but you can configure the output to compile to any directory you want.

Now you can deploy the contents of the build directory to your web server root.

Full documentation

https://godoc.org/github.com/leonelquinteros/thtml

Using the thtml/templates package

https://godoc.org/github.com/leonelquinteros/thtml/templates

Documentation

Overview

THTML is a static website generator based on text/template package.

Includes a development webserver to help creating HTML websites and components compiling the templates on the fight, allowing a edit-save-refresh development process.

Usage:

thtml [OPTIONS] [COMMAND]

[COMMAND] can be the following:

 -build
	    Build the assets from the [-public] directory to the [-output] directory by parsing the [-templates] directory.

 -run
	    Run development webserver listening to [-listen] to build pages on-the-fly.

[OPTIONS] are:

 -exts string
	    Provides a comma separated filename extensions list to support when parsing templates. (default ".thtml,.html,.css,.js")

 -listen string
	    Run the dev server listening on the provided host:port. (default ":5500")

 -minify
	    Minify the build output. (default true)

 -output string
	    Sets the path for the build output. (default "build")

 -public string
	    Sets the path for the web root. (default "public")

 -templates string
	    Sets the path for the template files. (default "templates")

Getting started

By running `thtml -run` on a directory, the tool will use the default options that assume the following directory structure:

./public
./templates
./build

The `public` directory and all its sub-directories will contain the source files for the template website structure, including assets that have to be included during the static website compilation. It works as the "web root" directory of the development webserver and contains the desired final structure.

The `templates` directory and all its sub-directories is where all the reusable templates have to be placed so they can be used from the pages in the `public` directory and from other templates.

The `build` directory will be used to write the output of the static website compilation and then deploy it.

Check the `_example` directory on the repository to see a simple layout: https://github.com/leonelquinteros/thtml/tree/master/_example

After creating the website using the development webserver, it can be built running `thtml -build` and the content of the `build` directory can be deployed to any static web server on production.

Template syntax

THTML uses pure Go's text/template package to render the templates together. Check the package documentation for details about the syntax: https://golang.org/pkg/text/template/

Directories

Path Synopsis
Package templates compiles all templates in a directory tree into a Service that provides a Render method to execute other templates inside its context.
Package templates compiles all templates in a directory tree into a Service that provides a Render method to execute other templates inside its context.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL