Inputs
There are two types of inputs. A jsonjson input contains structured data while a blobblob input passes bytes and optionally metadata to the template. For example, in an invoice the items and customer data could be a jsonjson input and the company logo could be a blobblob input.
Template inputs are configured in the manifest file typst.tomltypst.toml. The Oicana Typst package determines the current values of inputs.
Add the following to the top of your main.typmain.typ file to initialize the package:
This snippet gives the Oicana package access to the Typst project's files. We can now use the return values from calling setupsetup in the rest of the template.
We will use a jsonjson input to pass a name into the template. Add the following to the end of the typst.tomltypst.toml file:
The value of this input is now available in the template as input.infoinput.info, where infoinfo is the key of the input as defined in typst.tomltypst.toml.
While we develop the template, the value of the input will be nonenone, because there is no Oicana integration setting a value for it. We can change that by defining a defaultdefault or developmentdevelopment value for the input.
Inputs can define two different fallback values, defaultdefault and developmentdevelopment. These fallback values differ in priority based on which mode the template is compiled in.
When compiling a template in development mode, input values have the priority
- Explicit input value (for example through an integration)
developmentdevelopmentvaluedefaultdefaultvalue
If you compile in production mode, the developmentdevelopment value is ignored:
- Explicit input value (for example through an integration)
defaultdefaultvalue
While developing an Oicana template in a Typst editor, it will be compiled in development mode. It makes sense to define developmentdevelopment values for all required inputs of you template to have a functioning preview.
Let's extend our input with a developmentdevelopment value. First create an info.jsoninfo.json file in the template directory:
Then extend the input definition and set the developmentdevelopment value to be info.jsoninfo.json:
In our template we can now use input.info.nameinput.info.name and the preview will show "Chuck Norris".
With the input defined, we can update the packed template in the C# project. Run oicana packoicana pack in the template directory and replace example-0.1.0.zipexample-0.1.0.zip in the ASP.NET project with the new file.
Our compilecompile endpoint is currently calling var stream = template.Compile([], [], CompilationOptions.Pdf());var stream = template.Compile([], [], CompilationOptions.Pdf());. This compiles the template without any inputs. The first empty array are the jsonjson inputs and the second one the blobblob inputs.
Change the endpoint to set the name input we just defined.
Calling the endpoint now, will result in a PDF with "Baby Yoda". Building on this minimal service, one could set input values based on database entries or the request payload. Take a look at the open source ASP.NET example project on GitHub for a more complete showcase of the Oicana C# integration.