Tinderbox v10 Icon

Export: loops and joins

When exporting Tinderbox notes, the final document may be constructed from multiple notes. When the export is in the form of structured data, it is often necessary to place joining code between the output of individual being added to the overall export.

For example, in JSON data, lists of objects (i.e. per-note-derived data) require a comma between objects. The most obvious approach to do this is to use the export template append the joining character(s) at the end of each note's output. But that results a an extra unwanted (and possible non-format compliant) join after the last note's output.

So, in such a loop—in an export context—is it possible to test that there is not just another note yet to be export and, importantly, that said note is to be exported? Yes, using ^nextSibling^ (note too its counterpart ^previousSibling^).

As a general point, and perhaps counter-intuitively, it is better to test at start "am I second or more?" and prepend the join data, rather than test "am I last?" and append it. This is because it is easier to know definitely that a process has started as opposed to if/when it will end.

Still, export code lack the means for such a test. But an ^if()^ test's query can call a user function, so testing the state of first/last exportable note can be tested via a user function, and the work done in action code where things like contextual export state are more easily worked out.

See detecting first or last item context in loops and adding joins in loops for discussion of how to do such tests in action code.