How can I create linked tags from a comma separated list value in Stacey App?


I’d like to have tags/categories per project in Stacey like ‘design’, ‘photography’, ‘illustration’ etc, and have each be linked to their respective categories.

For example, my project.yml file reads:

title: Example Person's Logo
date: February 2014
client: Example Person
category: Logo, Digital,
content: +++
Branding for Example Person.

So, when in Stacey (v3) when in my project.html template I put {{ page.category }} it outputs Logo, Digital,, which is not what I’d like. It’d be fine if I wanted just text, but I don’t.

What I’d like is to grab the comma separated value (whether or not the last item has a comma), and turn it into this:

<a href="/category/logo/">Logo</a>
<a href="/category/digital/">Digital</a>

I’ve done some searching, and using the TWIG templating engine I’m unsure how this could be achieved without backreferences like in PHP. I’m told that in PHP the solution would be like this:

<?php echo preg_replace(
    "<a href='/tags/$1'>$1</a>",

However I don’t know how I could turn the comma separated list from ‘category:’ to an array, then strip the array of commas, and put the link tags around each while linking to the category in the href.

I’ve also tried using the above PHP as a partial, however dynamic code won’t work in Stacey due to the caching, and it can’t be referenced from outside the /partials/ folder.

I’m rather confused by all of this, any help would be hugely appreciated.

Note: Stacey doesn’t have the latest version of Twig which has filters like ‘split’.


You can use Twig’s split filter, then check if the element is not empty.

{% set categories = page.category|split(',') %}

{% for category in categories %}
    {% if category is not empty %}
        <a href="/category/{{ category|lower }}/">{{ category }}</a>
    {% endif %}
{% endfor %}

Since Stacy is still using version 1.4 at time of writing and that this filter was added to Twig in 1.10, you’ll need to add this filter yourself.

In extensions/, you can add a new filter to the extension’s getFilters array

public function getFilters() {
    # custom twig filters
    return array(

      //... other filters here ...

      'split' => new Twig_Filter_Method($this, 'split'),

Then after the public function getFunctions() method add this method

 * Custom filter method
 * @param string $str
 * @param string $delimiter
 * @return string[]
public function split($str, $delimiter) {
  return explode($delimiter, $str);

Answered By – Adam Elsodaney

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published