mirror of
				https://github.com/Dadido3/go-typst.git
				synced 2025-10-31 11:19:35 +00:00 
			
		
		
		
	Add example for passing go values
This commit is contained in:
		
							parent
							
								
									d03846864f
								
							
						
					
					
						commit
						f42f1cb385
					
				
							
								
								
									
										45
									
								
								examples/passing-objects/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								examples/passing-objects/main.go
									
									
									
									
									
										Normal 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) | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										
											BIN
										
									
								
								examples/passing-objects/output.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								examples/passing-objects/output.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										5
									
								
								examples/passing-objects/template-test-data.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								examples/passing-objects/template-test-data.typ
									
									
									
									
									
										Normal 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)), | ||||||
|  | ) | ||||||
							
								
								
									
										33
									
								
								examples/passing-objects/template.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								examples/passing-objects/template.typ
									
									
									
									
									
										Normal 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],)})], | ||||||
|  |     ) | ||||||
|  |   } | ||||||
|  | ) | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user