Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a transform function to list helper functions #827

Merged
merged 1 commit into from
Sep 4, 2014

Conversation

kuetemeier
Copy link
Contributor

What it does

With this litte change you can transform all values generated by the list helper functions.

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);
}
    <span class="category">{{ list_categories(article.categories, {
      show_count: false,
      show_count: false,
      class: 'article-category',
      style: 'none',
      separator: '►',
      transform: capitaliseFirstLetter
    }) }}</span>

and use it in your templates:

Simple example - Translations

use it with __() to translate things

    <span class="category">{{ list_categories(article.categories, {
      show_count: false,
      show_count: false,
      class: 'article-category',
      style: 'none',
      separator: '►',
      transform: __
    }) }}</span>

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:

    <span class="category">{{ list_categories(article.categories, {
      show_count: false,
      show_count: false,
      class: 'article-category',
      style: 'none',
      separator: '►',
      transform: category_transform(__)
    }) }}</span>

    <span class="category">{{ list_tags(article.tags, {
      show_count: false,
      class: 'article-category',
      style: 'none',
      separator: ', ',
      transform: tag_transform(__)
    }) }}</span>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants