Go library for creating efficient web backends powered by HttpRouter.
Check out the π examples directory for working project examples.
Important
Documentation of Go code can be found on the auto-generated API Reference website.
- High-performance request handling using object pools & pre-parsed documents.
- Type-safe template parsing to minimize rendering errors during requests.
- Automatic checksums and serving for static files.
Version 0.0.3:
- Print all exported
/staticendpoints. - Middleware for basic bot mitigation.
- Drop suspisious requests such as
GET /.env.
- Drop suspisious requests such as
- Improve error reporting: the highlighting is often incorrect, the error messages are WAY too long.
- Print tags syntax on errors
- More tests:
- Test FormatString.
- Test deeply nested tags.
- Test errors.
- Ensure paired tags are closed.
- Create 'static' tag.
- Preferably 'static' tag is declared by the core, and not part of w3html
Tip
There's a tree-sitter-html fork that adds support for W3 tags, which can be used for syntax highlighting in your editor of choice.
This section describes the syntax and features of the w3html templating language.
The HTML document is left as-is (without any parsing, syntax validation, indentation, etc.) other than parts inside or surrounded by W3 Tags.
W3 tags always query named arguments, but fall back to them in order (e.g. extend hello="world" path="hello.svg" is equivalent to extend "hello.svg" hello="world" as for extend tag definition).
| Type | Example |
|---|---|
| String | "Hello World", 'Hello World! |
| FormatString | `Hello {Path.To.Value()}` |
| Operation | ==, >=, < |
| LogicOperation | &&, || |
| Integer | 51, -9185 |
| Float | -.5, 31.8 |
| Bool | true, false |
Path must begin and be separated with a period .. Suffix a path segment with [] to iterate each element, () to call a method.
All paths are validated and baked during parsing using the provided datatype.
Example: .Path.To().Each[].Value()
If a path begins with a double period .., then the current scope will be appended (i.e. variable local to the parent tags).
If a path begins with a lowercase character it is instead replaced with the corresponding value provided in the parameters map (usually in extend or include tags).
Example: <span>{{ ._filename.base }}</span> could turn into <span>my_file.html</span> during parsing.
Built-in parameters include:
| Key | Value |
|---|---|
._filename |
The path to the root file as provided to os.OpenFile(...). A root file is the one that initiated NewParser(...).Parse() rather than any subsequent nested document |
._filename.base |
Same as ._filename, but with filepath.Base(...) applied to it, stripping the path and only keeping the filename |
{{ include <relative path to file> [key=value parameters...] }}
Provided path can ommit .html extension. HTML files are parsed and included by replacing the tag with nested document's contents. Provided key=value pairs can be accessed by using Compile-time Paths inside of the nested document.
{{ extend <relative path to file> [key=value parameters...] }}
...
{{ /extend }}
Similar to Include tag, but injects HTML inside of the nested document.
Refer to Slot and Place tags for injection documentation.
{{ place [slot id] }}
Indicates where the Extend tag may inject HTML using Slot tag.
Use {{ place "default" }} to allow for unnamed slot injection.
{{ slot [slot_id] }}
...
{{ /slot }}
Place inside of an Extend tag to specify a hunk of HTML to be injected in place of a Place tag.
{{ .Path.To[].Here() }}
Compute the path and replace the tag with the contents.
{{ range .Values[] }}
<span>{{ ..ItemValue }}</span>
{{ /range }}
{{ if .Value == 123 }}
<div>Hello World!</div>
{{ /if }}
Render contents only if the predicate is evaluated to true.
<head>
<link rel="stylesheet" href="{{ static 'styles/main.css' }}" />
</head>Evaluates into /static/<path>?checksum=<checksum> with a computed checksum. Used to force clients to re-download the files (cache invalidation on file change).
Tags are data-driven, meaning that it is possible to implement your own. Refer to TagEnum API Reference documentation.
var MyTag = TagEnum{
// Your properties here...
}
func init() {
w3html.RegisteredTags = append(w3html.RegisteredTags, MyTag)
}