Add example for passing go values

This commit is contained in:
David Vogel 2024-12-18 23:20:12 +01:00
parent d03846864f
commit f42f1cb385
4 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package main
import (
"log"
"os"
"time"
"github.com/Dadido3/go-typst"
)
// DataEntry contains fake data to be passed to typst.
type DataEntry struct {
Name string
Size struct{ X, Y, Z float64 }
Created time.Time
Numbers []int
}
var TestData = []DataEntry{
{Name: "Bell", Size: struct{ X, Y, Z float64 }{80, 40, 40}, Created: time.Date(2010, 12, 1, 12, 13, 14, 0, time.UTC), Numbers: []int{1, 2, 3}},
{Name: "Scissor", Size: struct{ X, Y, Z float64 }{200, 30, 10}, Created: time.Date(2015, 5, 12, 23, 5, 10, 0, time.UTC), Numbers: []int{4, 5, 10, 15}},
{Name: "Calculator", Size: struct{ X, Y, Z float64 }{150, 80, 8}, Created: time.Date(2016, 6, 10, 12, 15, 0, 0, time.UTC), Numbers: []int{16, 20, 30}},
{Name: "Key", Size: struct{ X, Y, Z float64 }{25, 10, 2}, Created: time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC), Numbers: []int{100, 199, 205}},
}
func main() {
typstCLI := typst.CLI{}
r, err := os.Open("template.typ")
if err != nil {
log.Panicf("Failed to open template file for reading: %v.", err)
}
defer r.Close()
f, err := os.Create("output.pdf")
if err != nil {
log.Panicf("Failed to create output file: %v.", err)
}
defer f.Close()
if err := typstCLI.RenderWithVariables(r, f, nil, map[string]any{"Data": TestData}); err != nil {
log.Panicf("Failed to render document: %v.", err)
}
}

Binary file not shown.

View File

@ -0,0 +1,5 @@
#let Data = (
(Name: "Bell", Size: (X: 80, Y: 40, Z: 40), Created: datetime(year: 2010, month: 12, day: 1, hour: 12, minute: 13, second: 14), Numbers: (1, 2, 3)),
(Name: "Bell", Size: (X: 80, Y: 40, Z: 40), Created: datetime(year: 2010, month: 12, day: 1, hour: 12, minute: 13, second: 14), Numbers: (10, 12, 15)),
(Name: "Bell", Size: (X: 80, Y: 40, Z: 40), Created: datetime(year: 2010, month: 12, day: 1, hour: 12, minute: 13, second: 14), Numbers: (100, 109, 199, 200)),
)

View File

@ -0,0 +1,33 @@
#set page(paper: "a4")
// Uncomment to use test data.
//#import "template-test-data.typ": Data
= List of items
#show table.cell.where(y: 0): strong
#set table(
stroke: (x, y) => if y == 0 {
(bottom: 0.7pt + black)
},
)
#table(
columns: 5,
table.header(
[Name],
[Size],
[Example box],
[Created],
[Numbers],
),
..for value in Data {
(
[#value.Name],
[#value.Size],
box(fill: black, width: 0.1mm * value.Size.X, height: 0.1mm * value.Size.Y),
value.Created.display(),
[#list(..for num in value.Numbers {([#num],)})],
)
}
)