Kevin Sadler Kevin Sadler - 2 months ago 27
Twig Question

In Pico CMS content markdown file how to define an array

In my simple Pico (0.9) based website I would like to add category pages that would list a set of objects, different for each category and defined in the markdown file for each category. In theory it should then be possible to list them in the twig template using the

{% for object in set %}
syntax.

I have happily been getting my own variables from the
.md
files using my own plugin which maps my custom
Key Value Pair Label:
onto a custom field in the
meta
variable in the template. But I just cannot get it to work with arrays.

Apparently the markdown header uses YAML and I have read the specification for that and there are a number of ways to specify such a list. So I have tried things like...

/*
Title: Example Category Page - Cutlery
My Items: ['fork', 'spoon', 'knife']
*/


Or this:

/*
Title: Example Category Page - Cutlery
My Items:
- fork
- spoon
- knife
*/


But when this is loaded in the template (with my plugin mapping it to
meta.my_items
) with code like this:

{% for item in meta.my_items %}
<p>{{ item }}</p>
{% endfor %}


Nothing is displayed as if the array
meta.my_items
is empty.

Ideally I would want to expand the objects in the array from simple strings to a set of KVPs (title, image, description) which I should be able to do in YAML.

So my question is - is this actually supported in Pico CMS and if so what am I doing wrong?

Answer

I tried (with the version 1.0) but the only working solution i found was about renaming it in the definition with an uderscore in the name. So try this:

My_Items: ['fork', 'spoon', 'knife']

instead of this:

My Items: ['fork', 'spoon', 'knife']

Hope this help.