How can we iterate over slice in metadata into HTML attribute value?

I would like to iterate over tags, delimit them with commas and insert them into the value of a HTML attribute.

<meta name="keywords" content="keyword1, keyword2, keyword3">

I tried reading through the docs and test but could not figure it out.

For the moment I know this workaround ispossible:

<ctx :if="$page.custom.get?('keywords')">
        <meta name="keywords" content="$if">
</ctx>

And in the page frontmatter:

.custom = {
     "keywords": "keyword1, keyword2, keyword3",
},

Would a function that takes arrays and returns a single delimited string planned?

Not requesting but wanting to know so don’t do this manually or script it :).

Thanks in advance.

1 Like

I think it would be nice to have PHP like implode and explode methods on the array struct to handle these kind of situations in scripty.

Implode:

<ctx :if="$page.custom.get?('meta_keywords')">
    <meta name="keywords" content="$if.implode(', ')">
</ctx>
.custom = {
    "meta_keywords": ["keyword1", "keyword2", "keyword3"],
},

Explode:

<ctx :if="$page.custom.get?('other_keywords')">
    <ul :loop="$page.custom.get('other_keywords').explode(', ')">
        <li :text="$if"></li>
    </ul>
</ctx>
.custom = {
    "other_keywords": "keyword1, keyword2, keyword3",
},
1 Like

I just implemented Array.join, it should become available for use in the next release.

I think the inverse operation is more niche so for now I haven’t implemented it, but with this the meta kewords usecase should be handled without needing any hack.

2 Likes

Thank you man, I really appreciate it.

1 Like