Breaking notes into groups within a single merge template

I’m trying to write a merge template that will break up a series of notes into groups less than or equal to n (let’s say 5). So for example, out of 15 notes, notes 1–5 would be output to file_1, notes 5–10 would be output to file_2, and so on…

I’ve managed to get the first 3 notes and the last 3 notes of a collection to be output using this code. I think that the notes between the first and last 3 are written to page_2.html but overwritten by successive calls somehow, until reaching the final 3 notes:

<?output "./test/page.html" ?>
<?set count = 0 ?>
<?nextrec?>
<?set count ++ 1 ?>
<?if =$count$= lt 4 ?>
<h1>=$title$=</h1>
<p>=$count$= / =$data-max$=</p>
=$body&o$=
<?endif?>
<?if =$count$= gt 4 ?>
<?set count = 0 ?>
<?output "./test/page_2.html" ?>
<?set count ++ 1 ?>
<h1>=$title$=</h1>
<p>=$count$= / =$data-max$=</p>
=$body&o$=
<?endif?>
<?loop?>

This is just some code that I brainstormed as a rudimentary representation of what I’m trying to achieve. I would prefer to use a single <?output "./test/page_=$VAR$=.html ?> command to output the grouped files, but I’m hung up on figuring out what variable to use to achieve this.

I reckon that my use of the set command may be off, or that I need to use a definegroup command somewhere, but I’m not sure how.

My key objective appears to be figuring out how to iterate over each note and process groups of files to the desired amount while maintaining a position in the overall note “index” (or [datacount](https://notenik.app/kb/system-variables.html)?), so that after one group of notes is output, I can pick up where I left off from (e.g., the sixth note in the data count, after outputting the first five notes) until the datamax is met.

In other words, I think that I’m looking to write something like a disembodied “for loop”…If this makes sense and anyone could provide a fine suggestion toward putting together this template as described, I would be much appreciative.

I would try something like this:

<?set file-number = 0  ?>
<?set note-count = 4 ?>
<?nextrec?>
<?set note-count += 1 ?>
<?if =$note-count$= eq 5 ?>
<?set file-number += 1 ?>
<?output "./test/page_=$file-number$=.html" ?>
<?set note-count = 0 ?>
<?endif?>
<h1>=$title$=</h1>
=$body&o$=
<?loop?>

Let me know how this works out.

This got the job done. Thank you for the intuitive solution, Herb.

  • It looks like <?set note-count = 4 ?> as opposed to a command like <?set note-count = 0 ?> is necessary because the latter will exclude notes 1–4 from the output file.

  • Setting the note count to four allows the first four notes to be rendered with the first group, otherwise the first group begins from the fifth note (<?if =$note-count$= eq 5 ?>), the output file is declared (<?output "./test/page_=$file-number$=.html" ?>) and then carries on as expected with notes 6–9 in tow. The remaining notes are grouped as expected also.

This assessment may be totally off the mark.

I think one thing that’s worth understanding is that the templating engine likely has no idea what note-count is or what the integer of four is referring to until it is called to increment the value immediately after the nextrec command, that happens to be represented by the note-count variable (my impression is that when a set command is called after nextrec, then the variable being set is immediately associated with the records being input).

I theorize that either:

  1. After making this “association”, the template engine knows to immediately register the first four notes. It can then increase by one in the following set command and carry on as written…

  2. All notes fed into the engine at the nextrec command and by initializing the note count to four, we’re able to ensure that an output is processed immediately after the fifth note…

To tell you the truth, I was almost certain that any code that would achieve my interests here would be something elaborate. My conclusion is that I underestimated the reach of merge commands, especially in conjunction with how the templating engine processes notes. It other words, Herb, I knew Notenik was a nifty piece of software in general, but the more I try to peak under the hood the more I grasp the notion—as an utter novice to the craft—that this is well-engineered software.

All in all, I’m just thinking out loud…

Thank you again!

1 Like

Part of the trickiness of setting the initial note-count to 4 is because we want the output command to execute after every five notes, but also need an initial execution. So by setting that count to 4, and then incrementing it after the nextrec command, we get that initial execution.

Whenever the output command is executed, it will close any file that is already open, and then open the new file. Any subsequent data that is generated will then be written to the output file that is currently open.

And then, at the end, any output file that is open will be closed.

Hope this helps some!

Thank you for explaining some of the internals to me, I’m hoping to put them to use for future templates. This insight helps a lot.