This, This and That

There may be times when you want to display the items in a “list”, as a comma separated string in a sentence structure.

In these instances, you will need to cater for the possibilities of there being “one” or “two” or “more than two” items in the list.

If there are more than two items, then commas will be required to separate the items with an “and” between the last two items.  If the items are being listed at the end of a sentence, then you will want a full stop after the last item.


Display a list as a comma separated string

These examples use a repeating section <<rs_ to loop through a list of colours, displaying each colour one after the other across the page, via the <<colour>> field.

These examples also reference two built-in-variables of Docmosis that are useful when looping through a list using <<rs_.:

$itemnum : This will be set to the number of the current item.
$size : This equals the total number of items in the list.

Conditional Sections <<cs_ can then used to check what item we are up to, when looping through the items in the list.  This allows us to know whether to put a “comma” or an “and” or a “fullstop” after the item.


Example 1

In this example, the conditional section <<cs_{$itemnum<$size}>> checks to see if the current colour is not the last colour in the list.

If the colour is not the last colour, then a comma followed by a space, will be placed after the item.


Example 2

The text “Colours:”, precedes the repeating section which displays all the colours in the list, separated by semicolons this time.

The fullstop shown after the <<es_colourList>> instruction is significant. This results in the fullstop being displayed at the end of the sentence, after completion of the repeating section which loops through all the colours in the list.


Example 3

This example goes one step further by showing an “and” between the last two colours.

This is achieved through another conditional section <<cs_{$itemnum=$size-1}>> which checks to see whether the current colour is the second last colour in the list.  If it is the second last colour, then it is followed by “ and ”.


Keep the listed first and last names together 

Sometimes lists contain paired values which should be kept together, with a comma separating each “paired item”.

A classic example is a list of people whereby the first and last names of every person should remain together, rather than split between lines, to ensure easy readability of a sentence.

To illustrate this, a list of all the members of a Committee has been used.


Example 4

In this more advanced example, we ensure that the first name and the last name of any committee member are not split between the end of one line and the beginning of the next line, through these steps:

  • Concatenate the first name and the last name, using the + operator in a Docmosis expression <<$name = {firstname + ‘ ’ + lastname}>>
  • Replace any spaces and hyphens which exist in the concatenated name with non-breaking spaces and non-breaking hyphens. For example, you do not want names like Jo Anne Smith or Mary-Anne Sauter or Kyle Peters-Smith split up over different lines because of the spaces and hyphens in their names.

At this point, you may want to turn on the hidden characters  in your word editor so you can see the visual difference between normal spaces and hyphens, and non-breaking space and hyphen characters.

Non-breaking space: Hold down the Ctrl and Shift keys as you press the space bar.
Non-breaking hyphen: Hold down the Ctrl and Shift keys as you press the hyphen key.

By invoking the Docmosis replace string function twice through this expression  {replace(replace($name,’ ‘,’ ‘),’-‘,’ ‘)} the normal space and hyphen characters are replaced with non-breaking space and hyphen characters.  

The resultant name string from the inner replace function with its non-breaking spaces, is passed to the outer replace function which effectively replaces any breaking hyphens with non-breaking hyphens.


Example 5

The code used in this example achieves the same result as Example 4. This alternative code structure may be considered more readable than the previous example which is written as a “single paragraph” to ensure that there are no unwanted line breaks in the resulting comma separated sentence.

In this second method, the sentence is “built up name by name” using the $line variable with the “complete sentence” printed after looping through all the “name items” in the list.


Note: If your version of LibreOffice displays grey spaces within the document for the non-breaking spaces, then these can be disabled with View>Field Shadings.


TRY: Changing the number of colours in the colour list and/or changing the names used in the committee list to see the effect on the output file/s. 


Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Invalid Input

Applies To

Docmosis-Java Tornado Cloud
Version(s) ALL ALL ALL

Related items