# Templating with Scriban

I want to create a PDF with dynamic data from a JSON object. I'm vaguely familiar with LaTeX and minimally familiar with Templating Engines and I'm struggling with how to implement the engine and shape my .tex file to accommodate it.

I've read the documentation of Scriban so I'm familiar with the syntax, but really any C# based Template Engine will work. I googled things like LaTeX with Scriban and even Using Template Engines with LaTeX.

I've looked into Generating LaTeX code with a template engine but he doesn't seem to get into the actual use of the template engine. Any references for template engines? The actual use of them inside of a tex file?

1. How do I use the variables in the .tex file
2. How do I tell my .tex file to expect these variables?

With Scriban you can write a template in the C# code and use variables inside the template. When you run the C# program the template will be rendered. The result of the rendering is LaTeX code, which you can save to a file from C#. Of course the template can also be written in a file that you load into a variable from your C# program.

Afterwards you need to compile the LaTeX file with a LaTeX compiler, which can of course be scripted, either from within your C# program or using an outside script that first runs your C# program and then LaTeX.

MWE:

using System;
using System.Collections.Generic;
using Scriban;

namespace LatexTemplate
{
class Program
{
static void Main(string[] args)
{
var template = Template.Parse(@"
\documentclass{article}
\begin{document}
\begin{itemize}
{{ for product in products }}\item {{product}}
{{ end }}\end{itemize}
\end{document}
");
var prod = new List<string>() { "apples", "pears", "cherries" };

var result = template.Render(new { Products = prod });
System.IO.File.WriteAllText(@"C:\myfolder\myfile.tex", result);
}
}
}


Resulting file myfile.tex:

\documentclass{article}
\begin{document}
\begin{itemize}
\item apples
\item pears
\item cherries
\end{itemize}
\end{document}


Compile with LaTeX:

Note that newlines in the template will be rendered as newlines in the LaTeX file, also newlines that are used to write for-loops, therefore in the example above the loop is spaced a bit strangely in order to produce nice-looking LaTeX code. Also indentations are rendered as-is, therefore the template is added all the way to the left.

• Thanks. I had a gross misunderstanding of how the template engine worked. LaTeX doesn't actually do anything...The engine reads the variables from the markup file and replaces those variables with whatever values it is programmed to do. Very nice answer, will be using it. – tblev May 18 '20 at 21:34