Export code to format string with each entry on a separate line using collect

Seems straightforward but having difficulty formatting my export code correctly.

Here is what I see with my incorrect export code:
image

What I want is each value entry on separate line like:
Uno
Dos
Tres
4
5
6

I tried this: ^value(collect(children(/Data),$Name.format("/n"))}^ but it did not seem to work.
I am including a sample file

Formatting Export Code.tbx (148.7 KB)

Thanks in advance
Tom

Tom, there are so many ways to do this. Here is one. Give me an hour and I’ll be uploading a new bullets list training. You’ll love it.


It is best to do this in a template and not in the body of a note.

Formatting Export CodeR2.tbx (147.0 KB)

1 Like
  1. We want first to collect the names of the children of Data:
collect(children(/Data),$Name)
  1. Note the parenthesis after $Name; it’s missing in your case. Instead, you’re collecting the formatted names, which is not what you meant to do.

  2. After you have collected these names, you want to format them with an intervening carriage return. The conventional symbol for carriage return is “\n”; you have written “/n”

collect(children(/Data),$Name) .format("\n")
  1. If you examine the results now in the Export pane, we see what we want to see:
 <body>
        Uno
Dos
Tres
4
5
6
    </body>

In the Preview pane, we still see Uno Dos Tres 4 5 6, because HTML ignores carriage returns. If we want to have items appear on separate lines in the preview pane, be need to insert the <br> (break) tag:

^value( collect(children(/Data),$Name).format("<br>") )
2 Likes

If you actually wanted the list in the $Text of the note, so that the list show birth in-app and in preview (for export) preview correct style, a rule/edict/stamp you this code is an approach:

$Text = collect(children(/Data),$Name).format("\n");

Now in-app we see:

whilst in preview mode we see the same:

So, lots of ways towards the same end. There is no wrong solution here (though there is code that doesn’t work!), but dependent on your task, some solutions will be more useful than others.

To explain further to @eastgate 's explanation above. …

The collect(scope, expressionStr) operator can take a (simple action) expression as its second argument. But, the list you are wishing to format doesn’t exist inside the operator. Rather it is the _output of the operator, with each child note (re children scope) contributing its $Name value as a single item of the eventual list. Thus the correction already offered above of chaining the .format() to the collect()` rather than to the $Name argument inside the operator.

Interestingly, I learned today that Tinderbox will parse this (note the space before the period of the dot-operator):

^value(collect(children(/Data),$Name) .format("/n"))^ 
                                     ^ space

as the intended code:

^value(collect(children(/Data),$Name).format("/n"))^ 

Ideally, don’t make the app guess as it works up until the point it doesn’t which by Murphy’s Law means in a context you can’t see/didn’t expect.

1 Like

Great learning case for me, as an export example. Thanks to all for the lessons.
MichaelB, @satikusala , I will be watching your session shortly. MarkA, @mwra , thanks for reminding me to use as a stamp as an alternative method. Good tip.

MarkB @eastgate , first of all, I hope you are feeling better! Thank you for the export tips. All have been written down in my learningTbx file and are being put to use as we speak.

Happy Holidays everyone and Happy New Year 2023!
Tom

1 Like

just a small remark - we are formatting a list and the semantically appropriate syntax would be (isn’t it nice that format allows this!):

^value(collect(children($Path),$Name).format("<ul>","<li>","</li>","</ul>"))^

If you don’t want to see the bullet in front of each list entry - you need to add some CSS like:

ul.no-bullets {
  list-style-type: none; /* Remove bullets */
  padding: 0; /* Remove padding */
  margin: 0; /* Remove margins */
}

and

^value(collect(children($Path),$Name).format("<ul class='no-bullets'>","<li>","</li>","</ul>"))^

Just another option for your problem.

Happy New Year!

2 Likes

Yes, you are absolutely correct. For more in this usage (i.e. 4 x arguments, not one, for List.format()), see List/Set.format(formatStr).

This 4-argument approach is an adoption in Action code of the method used in export codes like ^outboundBasicLinks( [start, list-item-prefix, list-item-suffix, end, type] )^, where such export codes cannot be used directly in a template and where HTML-marked-up lists need to be generated within an ^action()^.