I have a front end form with checkboxes which submits the data as an array into a plain text field in craft (named logGeneral_logInputIndications
).
Here's an example of what the content looks in the plain text field in Craft CP:
["Abdominal Pain","RUQ","Vomiting",""]
Instead of outputting the field content in it's default state into my template, I'd like to convert it to an unordered list. If I were to code it by hand it would look like this:
<ul>
<li>Abdominal Pain</li>
<li>RUQ</li>
<li>Vomiting</li>
</ul>
Any ideas if it's possible to do this?
(Mats answered the above question and his suggestion worked perfectly.)
However, I just realized that I have checkbox choices that contain non-alphanumeric content like Normal CBD (<0.4cm)
Is there a way to pick and choose which non-alphanumeric content I'd like to strip out vs keep?
Here's a new question that builds upon my initial one...
Is it possible to add a conditional to this? Some of the text is really a child so it should be inset by another unordered list. What my initial thought was is that perhaps I could add an identifier to the form input text if it's a child like "•" or something else, and then in the template, use that identifier in a conditional to say if you see "•" indent it and then strip the marker "•".
So then my plain text content would be:
["Abdominal Pain","•RUQ","Vomiting",""]
And my template code would look like this:
<ul>
<li>Abdominal Pain</li>
<ul>
<li>RUQ</li>
<ul>
<li>Vomiting</li>
</ul>
I was able to combine the suggestion Mats gave me with the following to conditionally indent child items.
{% set listItems = entry.logGeneral_logInputIndications|replace('/[^,()<>• a-zA-Z0-9_-]|[, ]$/s','')|split(',') %}
<ul>
{% if entry.logGeneral_logInputIndications|length %}
{% for listItem in listItems %}
{% if listItem starts with '•' %}
<ul><li>{{ listItem|trim|slice(1) }}</li></ul>
{% else %}
<li>{{ listItem|trim }}</li>
{% endif %}
{% endfor %}
{% endif %}
</ul>