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


i'd have tags/categories per project in stacey 'design', 'photography', 'illustration' etc, , have each linked respective categories.

for example, project.yml file reads:

title: example person's logo date: february 2014 client: example person category: logo, digital, content: +++ branding example person. +++ 

so, when in stacey (v3) when in project.html template put {{ page.category }} outputs logo, digital,, not i'd like. it'd fine if wanted text, don't.

what i'd grab comma separated value (whether or not last item has comma), , turn this:

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

i've done searching, , using twig templating engine i'm unsure how achieved without backreferences in php. i'm told in php solution this:

<?php echo preg_replace(     "#([^,\s]+)#is",     "<a href='/tags/$1'>$1</a>",     $row['tags']); ?> 

however don't know how turn comma separated list 'category:' array, strip array of commas, , put link tags around each while linking category in href.

i've tried using above php partial, dynamic code won't work in stacey due caching, , can't referenced outside /partials/ folder.

i'm rather confused of this, hugely appreciated.

note: stacey doesn't have latest version of twig has filters 'split'.

you can use twig's split filter, check if element not empty.

{% set categories = page.category|split(',') %}   {% category in categories %}     {% if category not empty %}         <a href="/category/{{ category|lower }}/">{{ category }}</a>     {% endif %} {% endfor %} 

since stacy still using version 1.4 @ time of writing , filter added twig in 1.10, you'll need add filter yourself.

in extensions/twig-extensions.inc.php, can add new filter extension's getfilters array

public function getfilters() {     # custom twig filters     return array(        //... other filters here ...        'split' => new twig_filter_method($this, 'split'),     ); } 

then after public function getfunctions() method add method

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

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -