QUICK XMERGE CODING
splitlines() Using text from a multifield
January 27, 2016 | matthew-townsend
But multifields already output…
Yes they do output text – but as a list and like so many things in life, requirements and what clients want can be diverse and often not limited to one type of output.
This quick xplain will look at how to extract the respective value and text options from a multi field so that you can then apply them in new and exciting ways but especially ways that avoid the need for hard coding.
Text and Values
Multi fields and Choice fields require both a ‘value’ and ‘text‘ input for each item added to the field.
You can see the respective breakdown on the image below:
Text items are what is always displayed on the interface and in your outputs – think of it as the client friendly part of the field.
Value is the behind the scenes data that the system actually relies on. Generally, you can change text aspects of a field – as long as the values remain the same – without consequence (templates, config pending) but changing the values will actually have an impact on things like interface conditioning or the linking and of course your xmerge.
In xmerge both of these are extractable by just adding .value or .text at the end of the field.
<:=$client.yes_no_maybe.value:> / <:=$client.valueOf(‘yes_no_maybe’):># 1 / 1
However as the name suggestions, multi-fields can have multiple selections and you will find applying the above logic to these fields produces some messy results. Therefore, we need to do a bit more tweaking to get that sweet sweet client friendly text output.
Extracting Text from a multi
To extract the text selections of a multi field we just need to use the string method ‘splitlines()’
Splitlines() returns a list result of each line of a string. Each selection of the multi field has a break or escape character built in, which allows the splitlines to break it down correctly when we iterate over it like below:
<:for item in $client.my_multifield_1.text.splitlines():>
# option 1
# option 3
So now we have a simple basis for extracting the text selections without having to use a lengthy and usually hard coded series of if statements. We can now take this decoupled text and use it in all manner of ways to deliver the outputs are clients are trying to achieve all without hard coding items and in a concisely coded manner.