### Install vfsgen and Build Example
Source: https://github.com/cloudykit/jet/blob/master/examples/asset_packaging/README.md
Installs the vfsgen tool and builds the application, compiling local assets into the binary.
```bash
go get -u "github.com/shurcooL/vfsgen"
make build
```
--------------------------------
### Run Example Application
Source: https://github.com/cloudykit/jet/blob/master/README.md
Navigate to the examples/todos directory and run the main Go application to see the Jet template engine in action.
```bash
cd examples/todos; go run main.go
```
--------------------------------
### Run Example Locally
Source: https://github.com/cloudykit/jet/blob/master/examples/asset_packaging/README.md
Runs the application locally, enabling hot-reloading of templates for development.
```bash
make run
```
--------------------------------
### Slice Array or Slice
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Re-slice a slice or array using Go-like [start:end] syntax. The element at the start index is included, and the one at the end index is excluded.
```go
{{ s := slice(6, 7, 8, 9, 10, 11) }}
{{ sevenEightNine := s[1:4] }}
```
--------------------------------
### Iterating with range over Slices
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Use range to iterate over slices. The context within the loop is the current element. You can optionally get the index.
```jet
{{ s := slice("foo", "bar", "asd") }}
{{ range s }}
{{.}}
{{ end }}
```
```jet
{{ range i := s }}
{{i}}: {{.}}
{{ end }}
```
```jet
{{ range i, v := s }}
{{i}}: {{v}}
{{ end }}
```
--------------------------------
### Recursive Block Invocation
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Allows a block to invoke itself, typically used for rendering hierarchical data structures. The example shows a menu structure with nested children.
```jet
{{ block menu() }}
{{ range . }}
{{ .Text }}{{ if len(.Children) }}{{ yield menu() .Children }}{{ end }}
{{ end }}
{{ end }}
```
--------------------------------
### Enable Asset Generation in main.go
Source: https://github.com/cloudykit/jet/blob/master/examples/asset_packaging/README.md
Adds a Go generate directive to main.go to trigger the asset packaging process.
```go
//go:generate go run assets/generate.go
```
--------------------------------
### Basic Delimiter Usage
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Demonstrates the default `{{` and `}}` delimiters for outputting context. Alternate delimiters like `[[` and `]]` can also be used.
```jet
hello {{ . }}
```
```jet
hello [[ . ]]
```
--------------------------------
### Basic Arithmetic Operations
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Supports basic arithmetic operators: +, -, *, /, %.
```go
{{ 1 + 2 * 3 - 4 }}
```
```go
{{ (1 + 2) * 3 - 4.1 }}
```
--------------------------------
### Variable Initialization
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Variables must be initialized using `:=` before they can be used in the template.
```jet
{{ foo := "bar" }}
```
--------------------------------
### Function Calls
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Call functions using C-like syntax or prefix syntax with a colon. Function calls using prefix syntax cannot be nested.
```go
{{ len(s) }}
```
```go
{{ isset(foo, bar) }}
```
```go
{{ len: s }}
```
```go
{{ isset: foo, bar }}
```
--------------------------------
### Pipelining
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Pipelining passes a value as the first argument to a function. Functions can be chained and accept additional parameters.
```go
{{ "123" | len}}
```
```go
{{ "FOO" | lower | len }}
```
```go
{{ "123" | lower | upper | len }}
```
```go
{{ "hello" | repeat: 2 | len }}
```
```go
{{ "hello" | repeat(2) | len }}
```
```go
{{ len(repeat("hello", 2)) }}
```
```go
{{ "hello" | upper | raw }}
```
```go
{{ raw: "hello" }}
```
--------------------------------
### Error handling with try
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Use try to attempt rendering content without crashing Jet if an error occurs. Content is only kept if the block executes successfully.
```jet
{{ try }}
we're not sure if we already initialised foo,
so the next line might fail...
{{ foo }}
{{ end }}
```
--------------------------------
### Include Template with Context
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Includes another template, making all local and global variables available. A context can be passed as the last argument; otherwise, the current context is preserved.
```jet
{{ .["name"] }}: {{ .["email"] }}
```
```jet
{{ range users }}
{{ include "./user.jet" }}
{{ end }}
```
```jet
{{ users := map(
"4243", map("name", "Peter", "email", "peter@aol.com"),
"4534", map("name", "Bob", "email", "bob@yahoo.com")
) }}
```
--------------------------------
### Renderer Interface
Source: https://github.com/cloudykit/jet/blob/master/docs/builtins.md
Details on the Renderer interface and its implementation for custom rendering.
```APIDOC
## Renderer
Jet exports a [`Renderer`](https://pkg.go.dev/github.com/CloudyKit/jet/v5?tab=doc#Renderer) interface (and [`RendererFunc`](https://pkg.go.dev/github.com/CloudyKit/jet/v5?tab=doc#RendererFunc) type which implements the interface). When an action evaluates to a value implementinng this interface, it will not be rendered using [fastprinter](https://github.com/CloudyKit/fastprinter), but by calling its Render() function instead.
### writeJson
`writeJson` renders the JSON encoding of whatever you pass in to the output, escaping only "<", ">", and "&" (just like the `json` function).
```
--------------------------------
### Logical Operators
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Supports logical operators: &&, ||, !, ==, !=, >, >=, <, <=. Expressions always evaluate to true or false.
```go
{{ item == true || !item2 && item3 != "test" }}
```
```go
{{ item >= 12.5 || item < 6 }}
```
--------------------------------
### Conditional Logic with if/else if/else
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Use if/else if/else for conditional rendering of content. This is equivalent to nested if/else structures.
```jet
{{ if foo == "asd" }}
foo is 'asd'!
{{ else if foo == 4711 }}
foo is 4711!
{{ else }}
foo is something else!
{{ end }}
```
```jet
{{ if foo == "asd" }}
foo is 'asd'!
{{ else }}
{{ if foo == 4711 }}
foo is 4711!
{{ else }}
foo is something else!
{{ end }}
{{ end }}
```
--------------------------------
### Import blocks from another Jet template
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Use the `import` statement to make blocks defined in another template available in the current template. All blocks from the imported template are made available.
```jet
{{ import "./my_blocks.jet" }}
{{ yield body() }}
```
--------------------------------
### String Concatenation
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Concatenate strings using the '+' operator.
```go
{{ "HELLO" + " " + "WORLD!" }}
```
--------------------------------
### Return Value from Template
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Sets a return value for a template, useful when executed with `exec()`. Execution of the current block or template is not stopped by `return`.
```jet
{{ f := "f" }}
{{ o := "o" }}
{{ return f+o+o }}
```
```jet
{{ foo := exec("./foo.jet") }}
Hello, {{ foo }}!
```
--------------------------------
### Define a Simple Block
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Defines a reusable block of template content. Blocks can accept arguments with optional default values. Defining a block also invokes it immediately unless `import` or `extends` is used.
```jet
{{block copyright()}}
{{ end }}
```
--------------------------------
### If-Else Statement
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Provides an alternative block to render when the primary `if` condition is false.
```go
{{ if foo == "asd" }}
foo is 'asd'!
{{ else }}
foo is something else!
{{ end }}
```
--------------------------------
### Call Exported Methods
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Call exported methods of Go types. This can be done on individual values or within a range loop.
```go
{{ user.Rename("Peter") }}
```
```go
{{ range users }}
{{ .FullName() }}
{{ end }}
```
--------------------------------
### Yield blocks in a layout template
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
In the base layout template, use the `yield` statement to specify where the content from the extending template's blocks should be rendered.
```jet
{{yield body()}}
```
--------------------------------
### If-Else If Statement
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Allows testing multiple conditions sequentially. The first condition that evaluates to true will have its block rendered.
```go
{{ if foo == "asd" }}
foo is 'asd'!
{{ else if foo == 4711 }}
foo is 4711!
{{ end }}
```
```go
{{ if foo == "asd" }}
foo is 'asd'!
{{ else }}
{{ if foo == 4711 }}
foo is 4711!
{{ end }}
{{ end }}
```
--------------------------------
### Error handling with try/catch
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Use try/catch to handle errors within a block. The catch block executes if an error occurs in the try block. Errors in catch are not caught.
```jet
{{ try }}
we're not sure if we already initialised foo,
so the next line might fail...
{{ foo }}
{{ catch }}
foo was not initialised, this is fallback content
{{ end }}
```
```jet
{{ try }}
we're not sure if we already initialised foo,
so the next line might fail...
{{ foo }}
{{ catch err }}
{{ log(err.Error()) }}
uh oh, something went wrong: {{ err.Error() }}
{{ end }}
```
--------------------------------
### Iterating with range over Channels
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Use range to iterate over channels. The two-variable syntax is required to keep the parent context.
```jet
{{ range v := c }}
{{v}}
{{ end }}
```
--------------------------------
### Slice/Array Indexing
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Retrieve elements from slices or arrays using `[]` notation with an index. The index can be a literal or a variable.
```jet
{{ s := slice("foo", "bar", "asd") }}
{{ s[0] }}
{{ i := 2 }}
{{ s[i] }}
```
--------------------------------
### Map Indexing
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Access values in a map using `[]` notation with a key. Keys can be literals or variables.
```jet
{{ m := map("foo", 123, "bar", 456) }}
{{ m["foo"] }}
{{ bar := "bar" }}
{{ m[bar] }}
```
--------------------------------
### Extend a Jet template
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Use the `extends` statement at the top of a template to inherit from a base layout. Content outside of block definitions in the extending template will be discarded.
```jet
{{extends "./layout.jet"}}
```
--------------------------------
### Define content for an extended template
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Define blocks in the extending template to provide content that will be yielded in the base layout. Content outside of blocks will not be rendered.
```jet
{{extends "./layout.jet"}}
{{block body()}}
This content can be yielded anywhere.
{{end}}
This content will never be rendered.
```
--------------------------------
### Access Map Fields
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Access map fields using dot notation. When the identifier before the dot is omitted, the field is looked up in the current context.
```go
{{ m := map("foo", 123, "bar", 456) }}
{{ m.foo }}
```
```go
{{ s := slice(m, map("foo", 4711))
{{ range s }}
{{ .foo }}
{{ end }}
```
--------------------------------
### If Statement
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Conditionally render content based on a boolean expression. The block is rendered only if the condition is true.
```go
{{ if foo == "asd" }}
foo is 'asd'!
{{ end }}
```
--------------------------------
### SafeWriter Utilities
Source: https://github.com/cloudykit/jet/blob/master/docs/builtins.md
Information on SafeWriter functions for controlling output escaping.
```APIDOC
## SafeWriter
Jet includes a [`SafeWriter`](https://pkg.go.dev/github.com/CloudyKit/jet/v5?tab=doc#SafeWriter) function type for writing directly to the render output stream. This can be used to circumvent Jet's default HTML escaping. Jet has a few such functions built-in.
### safeHtml
`safeHtml` is an alias for Go's [template.HTMLEscape](https://golang.org/pkg/text/template/#HTMLEscape) (converted to the `SafeWriter` type). This is the same escape function that's also applied to the evalutation result of action nodes by default. It escapes everything that could be interpreted as HTML.
### safeJs
`safeJs` is an alias for Go's [template.JSEscape](https://golang.org/pkg/text/template/#JSEscape). It escapes data to be safe to use in a Javascript context.
### raw/unsafe
`raw` (alias `unsafe`) is a writer that escapes nothing at all, allowing you to circumvent Jet's default HTML escaping. Use with caution!
```
--------------------------------
### Variable Assignment
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Initialized variables can be reassigned new values, including different types. Assigning to `_` executes the right side but skips assignment.
```jet
{{ foo = "asd" }}
```
```jet
{{ foo = 4711 }}
```
```jet
{{ _ := stillRuns() }}
```
```jet
{{ _ = stillRuns() }}
```
--------------------------------
### Whitespace Trimming with Delimiters
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Use `{{- ` and ` -}}` to trim whitespace around delimiters, useful for formatting template source code. Whitespace includes spaces, tabs, carriage returns, and newlines.
```jet
foo {{ "bar" }} baz
```
```jet
foo {{- "bar" -}} baz
```
--------------------------------
### Invoke a Defined Block
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Invokes a previously defined block. Parameters without defaults must be provided. Context can be passed explicitly or the current context is used.
```jet
```
```jet
{{yield inputField(id="firstname", label="First name", required=true)}}
```
```jet
{{block buff()}}
{{.}}
{{end}}
{{yield buff() "Batman"}}
```
--------------------------------
### Piped Argument Slot
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Use `_` to specify where the piped value should be injected in a function call when it's not the first argument. This allows for more complex argument passing.
```go
{{ 2 | repeat("foo", _) }}
```
```go
{{ 2 | repeat("foo", _) | repeat(_, 3) }}
```
```go
{{ 2 | repeat("foo", _) | repeat(3) }}
```
```go
{{ 2 | repeat: "foo", _ | repeat: 3 }}
```
```go
{{ 2 | repeat: "foo", _ | repeat: _, 3 }}
```
--------------------------------
### Iterating with range over Maps
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Use range to iterate over maps. You can access the key and value, and the parent context remains available.
```jet
{{ m := map("foo", "bar", "asd", 123)}}
{{ range k := m }}
{{k}}: {{.}}
{{ end }}
```
```jet
{{ range k, v := m }}
{{k}}: {{v}}
{{ end }}
```
--------------------------------
### String Indexing
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Access individual bytes of a string using `[]` notation. The result is the ASCII value of the character.
```jet
{{ s := "helloworld" }}
{{ s[1] }}
```
--------------------------------
### Define reusable blocks in a Jet template
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Define blocks in a separate template that can be imported and yielded in other templates. These blocks encapsulate reusable content.
```jet
{{ block body() }}
This content can be yielded anywhere.
{{ end }}
```
--------------------------------
### Built-in Functions
Source: https://github.com/cloudykit/jet/blob/master/docs/builtins.md
Details on various built-in functions available in Jet, including string manipulation, length checking, and execution.
```APIDOC
## Built-in Functions
### From Go
The following functions simply expose functions from Go's standard library for convenience:
- `lower`: exposes Go's [strings.ToLower](https://golang.org/pkg/strings/#ToLower)
- `upper`: exposes Go's [strings.ToUpper](https://golang.org/pkg/strings/#ToUpper)
- `hasPrefix`: exposes Go's [strings.HasPrefix](https://golang.org/pkg/strings/#HasPrefix)
- `hasSuffix`: exposes Go's [strings.HasSuffix](https://golang.org/pkg/strings/#HasSuffix)
- `repeat`: exposes Go's [strings.Repeat](https://golang.org/pkg/strings/#Repeat)
- `replace`: exposes Go's [strings.Replace](https://golang.org/pkg/strings/#Replace)
- `split`: exposes Go's [strings.Split](https://golang.org/pkg/strings/#Split)
- `trimSpace`: exposes Go's [strings.TrimSpace](https://golang.org/pkg/strings/#TrimSpace)
- `html`: exposes Go's [html.EscapeString](https://golang.org/pkg/html/#EscapeString)
- `url`: exposes Go's [url.QueryEscape](https://golang.org/pkg/net/url/#QueryEscape)
- `json`: exposes Go's [json.Marshal](https://golang.org/pkg/encoding/json/#Marshal)
### len
`len()` takes one argument and returns the length of a string, array, slice or map, the number of fields in a struct, or the buffer size of a channel, depending on the argument's type. (Think of it like Go's `len()` function.)
It panics if you pass a value of any type other than string, array, slice, map, struct or channel.
`len()` indirects through arbitrary layers of pointer and interface types before checking for a valid type.
### isset
`isset()` takes an arbitrary number of index, field, chain or identifier expressions and returns true if all expressions evaluate to non-nil values. It panics only when an unexpected expression type is passed in.
### exec
`exec()` takes a template path and optionally a value to use as context and executes the template with the current or specified context. It returns the last value returned using the `return` statement, or nil if no `return` statement was executed.
### ints
`ints()` takes two integers as lower and upper limit and returns a Ranger producing all the integers between them, including the lower and excluding the upper limit. It panics when the arguments can't be converted to integers or when the upper limit is not strictly greater than the lower limit.
### dump
`dump` is meant to aid in template development, and can be used to print out variables, blocks, context, and globals that are available to the template.
The function can be used in three forms:
`dump()` used without parameters will print out context, variables, globals, and blocks (in this order) in the current scope, without accessing any parent.
`dump(levels)` - where `levels` is an **integer** - is the same as `dump()`, but will additionally recurse over context parents to the maximum depth of `levels`.
For example, `dump(1)` will additionaly print out all variables accessible in the direct parent of the current context.
`dump("name1", "name2", ...)` will search for the variable and/or block with the given name(s) in any scope (current and all parents) of the current runtime.
```
--------------------------------
### Access Struct Fields
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Access struct fields using dot notation. This works for individual struct values and within ranges iterating over slices of structs.
```go
{{ user.Name }}
```
```go
{{ range users }}
{{ .Name }}
{{ end }}
```
--------------------------------
### Ternary Operator
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
The ternary operator `x ? y : z` evaluates to `y` if `x` is truthy, otherwise it evaluates to `z`.
```go
{{ .HasTitle ? .Title : "Title not set" }}
```
--------------------------------
### Range with else block
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
An else block can be added to range statements to execute when there are no values to iterate over, such as an empty slice or closed channel.
```jet
{{ range searchResults }}
{{.}}
{{ else }}
No results found :(
{{ end }}
```
--------------------------------
### Jet Comments
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Comments are defined using `{*` and `*}` and are removed during template parsing. They can span multiple lines.
```jet
{* this is a comment *}
```
```jet
{*
none of this will be executed:
{{ asd }}
{{ include "./foo.jet" }}
*}
```
--------------------------------
### Block with Inner Content Rendering
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Designates where inner content passed during a `yield` should be rendered within a block using `yield content`. The variable scope is restored when content is executed.
```jet
{{ block link(target) }}
{{ yield content }}
{{ end }}
```
```jet
{{ yield link(target="https://www.example.com") content }}
Example Inc.
{{ end }}
```
```jet
{{ name := "Sarah" }}
{{ block header() }}
{{ yield content }}
{{ content }}
Hey {{ name }}!
{{ end }}
```
--------------------------------
### Struct Field Access
Source: https://github.com/cloudykit/jet/blob/master/docs/syntax.md
Access fields of a struct using `[]` notation with the field name. This is shown for map-like access to struct fields.
```jet
{{ user["Name"] }}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.