hexojs/hexo — pull request #827 by jkuetemeier (2014-09-04 10:56:15+00:00)
Title: Added a transform function to list helper functions

## What it does
With this litte change you can transform all values generated by the list helper function.

### Default behavior

nothing changed, because we use a simple default function:
```
// default: do not transform the value
transform_default = function(value) { return value; };
```

### Simple example - Uppercase first letter

Define a helper function (for full example see below)

```
function capitaliseFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
```

```
{{ list_categories(article.categories, {
show_count: false,
show_count: false,
class: 'article-category',
style: 'none',
separator: '►',
transform: capitaliseFirstLetter
}) }}
```

and use it in your templates:

### Simple example - Translations

use it with `__()` to translate things

```
{{ list_categories(article.categories, {
show_count: false,
show_count: false,
class: 'article-category',
style: 'none',
separator: '►',
transform: __
}) }}
```

## Full example

How to use it to translate categories, tags and more (or just give tags and categories a 'slug'). Use it like this:

in `theme/yourtheme/script/extend.js`

```
hexo.extend.helper.register('category_transform', function(helper) {
var f = function(s) {
return helper('category_'+s);
}
return f;
})

hexo.extend.helper.register('tag_transform', function(helper) {
var f = function(s) {
return helper('tag_'+s);
}
return f;
})
```

in `theme/yourtheme/languages/default.yml`

```
category_test: Test
category_it-security: IT-Sicherheit

tag_mac: Mac OS X
tag_linux: Linux
tag_javascript: Java-Script
tag_itsecurity: IT-Sicherheit
```

e.g. in a swig template:

```
{{ list_categories(article.categories, {
show_count: false,
show_count: false,
class: 'article-category',
style: 'none',
separator: '►',
transform: category_transform(__)
}) }}

{{ list_tags(article.tags, {
show_count: false,
class: 'article-category',
style: 'none',
separator: ', ',
transform: tag_transform(__)
}) }}
```