MCPcopy Create free account
hub / github.com/CloudyKit/jet / ExampleRanger

Function ExampleRanger

ranger_test.go:36–69  ·  view source on GitHub ↗

ExampleRanger demonstrates how to write a custom template ranger.

()

Source from the content-addressed store, hash-verified

34
35// ExampleRanger demonstrates how to write a custom template ranger.
36func ExampleRanger() {
37 // Error handling ignored for brevity.
38 //
39 // Setup template and rendering.
40 loader := NewInMemLoader()
41 loader.Set("template",
42 `{{ range k := ecr }}
43{{k}} = {{.}}
44{{- end }}
45{{ range k := struct.RangerEface }}
46{{k}} = {{.}}
47{{- end }}`,
48 )
49 set := NewSet(loader, WithSafeWriter(nil))
50 t, _ := set.GetTemplate("template")
51
52 // Pass a custom ranger instance as the 'ecr' var, as well as in a struct field with type interface{}.
53 vars := VarMap{
54 "ecr": reflect.ValueOf(&exampleCustomRanger{}),
55 "struct": reflect.ValueOf(struct{ RangerEface interface{} }{RangerEface: &exampleCustomRanger{}}),
56 }
57
58 // Execute template.
59 _ = t.Execute(os.Stdout, vars, nil)
60
61 // Output:
62 // 0 = custom ranger 0
63 // 1 = custom ranger 1
64 // 2 = custom ranger 2
65 //
66 // 0 = custom ranger 0
67 // 1 = custom ranger 1
68 // 2 = custom ranger 2
69}

Callers

nothing calls this directly

Calls 6

SetMethod · 0.95
GetTemplateMethod · 0.95
NewInMemLoaderFunction · 0.85
NewSetFunction · 0.85
WithSafeWriterFunction · 0.85
ExecuteMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…