Skip to content

Commit

Permalink
docs: Remove TODO from ranger section
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusd authored and sauerbraten committed Dec 16, 2022
1 parent 0d2bc22 commit 0683ae6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
11 changes: 9 additions & 2 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [Slices / Arrays](#slices--arrays)
- [Maps](#maps)
- [Channels](#channels)
- [Custom](#custom-ranger)
- [else](#else)
- [try](#try)
- [try / catch](#try--catch)
Expand Down Expand Up @@ -366,7 +367,7 @@ Use `range` to iterate over data, just like you would in Go, or how you would us
{{.}}
{{ end }}

Jet provides built-in rangers for Go slices, arrays, maps, and channels. You can add your own by implementing the Ranger interface. TODO
Jet provides built-in rangers for Go slices, arrays, maps, and channels. You can add your own by implementing the Ranger interface.

#### Slices / Arrays

Expand Down Expand Up @@ -411,6 +412,12 @@ When iterating over a channel, you can can have Jet assign the iteration value t

It's an error to use channels together with the two-variable syntax.

#### Custom Ranger

Any value that implements the
[Ranger](https://pkg.go.dev/github.com/CloudyKit/jet/v6#Ranger) interface can be
used for ranging over values. Look in the package docs for an example.

#### else

`range` statements can have an `else` block which is executed if there are non values to range over (as signalled by the Ranger). For example, it will run when iterating an empty slice, array or map or a closed channel:
Expand Down Expand Up @@ -687,4 +694,4 @@ Executing `index.jet` will produce:

`import` makes all the blocks from the imported template available in the importing template. There is no way to only import (a) specific block(s).

Since the imported template isn't actually executed, the blocks defined in it don't run until you `yield` them explicitely.
Since the imported template isn't actually executed, the blocks defined in it don't run until you `yield` them explicitely.
52 changes: 52 additions & 0 deletions ranger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package jet

import (
"fmt"
"os"
"reflect"
)

// exampleCustomBenchRanger satisfies the Ranger interface, generating fixed
// data.
type exampleCustomRanger struct {
i int
}

// Type assertion to verify exampleCustomRanger satisfies the Ranger interface.
var _ Ranger = (*exampleCustomRanger)(nil)

func (ecr *exampleCustomRanger) ProvidesIndex() bool {
// Return false if 'k' can't be filled in Range().
return true
}

func (ecr *exampleCustomRanger) Range() (k reflect.Value, v reflect.Value, done bool) {
if ecr.i >= 3 {
done = true
return
}

k = reflect.ValueOf(ecr.i)
v = reflect.ValueOf(fmt.Sprintf("custom ranger %d", ecr.i))
ecr.i += 1
return
}

// ExampleRanger demonstrates how to write a custom template ranger.
func ExampleRanger() {
// Error handling ignored for brevity.
//
// Setup template and rendering.
loader := NewInMemLoader()
loader.Set("template", "{{range k := ecr }}{{k}}:{{.}}; {{end}}")
set := NewSet(loader, WithSafeWriter(nil))
t, _ := set.GetTemplate("template")

// Pass a custom ranger instance as the 'ecr' var.
vars := VarMap{"ecr": reflect.ValueOf(&exampleCustomRanger{})}

// Execute template.
_ = t.Execute(os.Stdout, vars, nil)

// Output: 0:custom ranger 0; 1:custom ranger 1; 2:custom ranger 2;
}

0 comments on commit 0683ae6

Please sign in to comment.