|
| 1 | +````markdown |
| 2 | +# Neutral TS IPC Client Go |
| 3 | + |
| 4 | +**IPC Client Go is experimental.** |
| 5 | + |
| 6 | +This package provides a minimal Go client for Neutral TS templates using the IPC server. |
| 7 | + |
| 8 | +The client exposes `NeutralIpcTemplate` which lets you render a template by sending a schema |
| 9 | +and template path (or source) to the IPC server and receiving the rendered content. |
| 10 | + |
| 11 | +Example |
| 12 | +------- |
| 13 | + |
| 14 | +```go |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "path/filepath" |
| 21 | + "runtime" |
| 22 | + "os" |
| 23 | + |
| 24 | + // Import the Neutral IPC client package. |
| 25 | + // The client sources are available at: |
| 26 | + // https://github.com/FranBarInstance/neutral-ipc/tree/master/clients |
| 27 | + // Replace the import path below according to your module layout. |
| 28 | + "neutral_ipc_template" |
| 29 | +) |
| 30 | + |
| 31 | +func main() { |
| 32 | + // The schema contains the data and variables for the template |
| 33 | + schema := map[string]interface{}{ |
| 34 | + "data": map[string]interface{}{ |
| 35 | + "hello": "Hello World", |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + // Convert schema to JSON string |
| 40 | + schemaBytes, _ := json.Marshal(schema) |
| 41 | + schemaJSON := string(schemaBytes) |
| 42 | + |
| 43 | + // Determine the template full path (template.ntpl should be next to this README) |
| 44 | + _, b, _, _ := runtime.Caller(0) |
| 45 | + dir := filepath.Dir(b) |
| 46 | + template := filepath.Join(dir, "template.ntpl") |
| 47 | + |
| 48 | + // Create a NeutralIpcTemplate instance |
| 49 | + ipc := neutral_ipc_template.NewNeutralIpcTemplate(template, schemaJSON) |
| 50 | + |
| 51 | + // Render the template |
| 52 | + contents := ipc.Render() |
| 53 | + |
| 54 | + // e.g.: 200 |
| 55 | + statusCode := ipc.GetStatusCode() |
| 56 | + |
| 57 | + // e.g.: OK |
| 58 | + statusText := ipc.GetStatusText() |
| 59 | + |
| 60 | + // empty if no error |
| 61 | + statusParam := ipc.GetStatusParam() |
| 62 | + |
| 63 | + // Act according to your framework to display the content |
| 64 | + // for this example, simply output |
| 65 | + fmt.Println(contents) |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Links |
| 70 | +----- |
| 71 | + |
| 72 | +Neutral TS template engine. |
| 73 | + |
| 74 | +- [Template docs](https://github.com/FranBarInstance/neutralts-docs/docs/neutralts/doc/) |
| 75 | +- [Repository](https://github.com/FranBarInstance/neutralts) |
| 76 | +- [Crate](https://crates.io/crates/neutralts) |
| 77 | +- [Examples](https://github.com/FranBarInstance/neutralts-docs/tree/master/examples) |
0 commit comments