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

Open
opened 3 years ago by RenaissanceBug · 3 comments
RenaissanceBug commented 3 years ago (Migrated from github.com)

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.

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 3 years ago (Migrated from github.com)

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.

> 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`](https://docs.racket-lang.org/simple-xlsx/index.html) or [`csv-reading`](https://docs.racket-lang.org/csv-reading/index.html)) 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](https://docs.racket-lang.org/beeswax/index.html) template lang of use for this rendering step.
mbutterick commented 3 years ago (Migrated from github.com)

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.

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 3 years ago (Migrated from github.com)

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.

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.
This repo is archived. You cannot comment on issues.
No Milestone
No project
No Assignees
1 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/pollen-users#125
Loading…
There is no content yet.