mbutterick / pollen-users

please use https://forums.matthewbutterick.com/c/typesetting/ instead
https://forums.matthewbutterick.com/c/typesetting/
52 stars 0 forks source link

Filling templates multiple times, à la mail-merge -- maybe a non-use case? #125

Open RenaissanceBug opened 2 years ago

RenaissanceBug commented 2 years ago

Is there a way in Pollen (or a Scribble layer reachable from Pollen) to go from [an xexpr template] to [a function that renders the template for a given output-filename and given template inputs]?

For part of my site, I have an xexpr template I'd like to fill in multiple times, rendering each copy to HTML and PDF linked from a main page. The data to populate the template is coming from an Excel sheet, and the file names are derivable from the data. So this is very much like doing an old-fashioned mail-merge, just with data that's too structurally rich for kludging together a solution with fields in a Word doc.

The fact that all the render functions in Pollen work from some form of path makes me think I'm barking up the wrong tree here. I could, say, generate a bunch of .html.pm files that all use the same template...but that feels like a graceless way to do it. So, I'm suspecting Pollen may not be the right tool in the Racket/Scribble ecosystem for what I want, but I also suspect someone in this community will be easily able to point me in a likely right direction if so.

otherjoel commented 2 years ago

I could, say, generate a bunch of .html.pm files that all use the same template...but that feels like a graceless way to do it. So, I'm suspecting Pollen may not be the right tool in the Racket/Scribble ecosystem for what I want

Pollen is designed as a “publishing system for authors”, so if you just want to transform data without an intermediate authoring step then you may not need Pollen.

The most direct way to do what you want in Racket would be to write a function that ingests your spreadsheet data (e.g. using simple-xlslx or csv-reading) and produces a list of records in whatever intermediate format you want (structs or x-expressions, doesn’t matter). Then write a function that can render a record in that intermediate format into HTML and a function that does the same for PDF (likely via LaTeX or similar). Shameless self-plug, you may find my Beeswax template lang of use for this rendering step.

mbutterick commented 2 years ago

In addition to @otherjoel’s suggestions, an approach I’ve used in similar cases involves turning the “mail merge” fields into Racket parameters. Rather than relying on raco pollen render ···, you’d trigger the rendering using a helper script, using a single merge.html.pm file with parameterize to change the field values on each pass. After each render, change the name of the resulting merge.html to whatever you want. Repeat loop until done.

RenaissanceBug commented 2 years ago

Thanks for the replies! I'll play with this some more, but between your two suggestions I should be able to make something work easily enough.