Skip to content

Commit 5afdd47

Browse files
committed
update docs on SafeWriter and Renderer built-ins
1 parent 53450f2 commit 5afdd47

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

default.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ func unsafePrinter(w io.Writer, b []byte) {
170170
w.Write(b)
171171
}
172172

173-
// SafeWriter escapee func. Functions implementing this type will write directly into the writer,
174-
// skipping the escape phase; use this type to create special types of escapee funcs.
173+
// SafeWriter is a function that writes bytes directly to the render output, without going through Jet's auto-escaping phase.
174+
// Use/implement this if content should be escaped differently or not at all (see raw/unsafe builtins).
175175
type SafeWriter func(io.Writer, []byte)
176176

177177
var stringType = reflect.TypeOf("")

docs/builtins.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
- [isset](#isset)
77
- [exec](#exec)
88
- [ints](#ints)
9+
- [SafeWriter](#safewriter)
10+
- [safeHtml](#safehtml)
11+
- [safeJs](#safejs)
12+
- [raw/unsafe](#rawunsafe)
13+
- [Renderer](#renderer)
14+
- [writeJson](#writejson)
915

1016
## Functions
1117

@@ -23,6 +29,7 @@ The following functions simply expose functions from Go's standard library for c
2329
- `trimSpace`: exposes Go's [strings.TrimSpace](https://golang.org/pkg/strings/#TrimSpace)
2430
- `html`: exposes Go's [html.EscapeString](https://golang.org/pkg/html/#EscapeString)
2531
- `url`: exposes Go's [url.QueryEscape](https://golang.org/pkg/net/url/#QueryEscape)
32+
- `json`: exposes Go's [json.Marshal](https://golang.org/pkg/encoding/json/#Marshal)
2633

2734
### len
2835

@@ -42,4 +49,28 @@ It panics if you pass a value of any type other than string, array, slice, map,
4249

4350
### ints
4451

45-
`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.
52+
`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.
53+
54+
## SafeWriter
55+
56+
Jet includes a [`SafeWriter`](https://pkg.go.dev/github.com/CloudyKit/jet/[email protected]?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.
57+
58+
### safeHtml
59+
60+
`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.
61+
62+
### safeJs
63+
64+
`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.
65+
66+
### raw/unsafe
67+
68+
`raw` (alias `unsafe`) is a writer that escapes nothing at all, allowing you to circumvent Jet's default HTML escaping. Use with caution!
69+
70+
## Renderer
71+
72+
Jet exports a [`Renderer`](https://pkg.go.dev/github.com/CloudyKit/jet/[email protected]?tab=doc#Renderer) interface (and [`RendererFunc`](https://pkg.go.dev/github.com/CloudyKit/jet/[email protected]?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.
73+
74+
#### writeJson
75+
76+
`writeJson` renders the JSON encoding of whatever you pass in to the output, escaping only "<", ">", and "&" (just like the `json` function).

eval.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ var (
4141
}
4242
)
4343

44-
// Renderer any resulting value from an expression in an action that implements this
45-
// interface will not be printed, instead, we will invoke his Render() method which will be responsible
46-
// to render his self
44+
// Renderer is used to detect if a value has its own rendering logic. If the value an action evaluates to implements this
45+
// interface, it will not be printed using github.com/CloudyKit/fastprinter, instead, its Render() method will be called
46+
// and is responsible for writing the value to the render output.
4747
type Renderer interface {
4848
Render(*Runtime)
4949
}

0 commit comments

Comments
 (0)