Mecha CMS

Mecha CMS blog and documentation.

Thought About Categories

Updated: Sunday, 07 August 2016

Is this necessary?

Update: The plugin version of this tutorial is now available fo Mecha 1.2.6 and above.

I’m thinking on adding a category feature in Mecha. Categories are similar with tags, the only difference is that you can only add one category per article. This is what I’m thinking on how to format the file name in the future. This is backward compatible, I think:

  • 2015-07-12-12-10-05 is the article time
  • C1 is the category ID
  • 1, 2, 3, 4 are the tags ID
  • article-slug is the article slug.

But, so far the tag system is basically enough to classify the articles in a simple Mecha–based blog. So I think this feature will be more suitable for a plugin. Some possible workaround for this:

Category Custom Field

return array(
    'category' => array(
        'title' => 'Category',
        'type' => 'option',
        'value' => array(
            'C0' => 'None',
            'C1' => 'Shōnen',
            'C2' => 'Shōjo',
            'C3' => 'Seinen',
            'C4' => 'Josei',
            'C5' => 'Hentai',
        'scope' => 'article',
        'description' => 'Specify a category for this article.'

Rename File on Article Update

Weapon::add('on_article_update', function($G, $P) {
    $data = $P['data'];
    if(isset($data['fields']['category'])) {
        $id = $data['fields']['category'];
        $path = $data['path'];
        $name = File::B($path);
        if( ! Text::check($name)->has('_C')) {
            $name = preg_replace('#_\d+(?=[,_])#', '_' . $id . ',', $name);
        } else {
            $name = preg_replace('#_C\d+(?=[,_])#', '_' . $id, $name);

Sample Route

Route::accept(array('category/(:any)', 'category/(:any)/(:num)'), function($slug = "", $offset = 1) {

Possible New Methods

// `Get::categories()`
Get::plug('categories', function() { … });

// `Get::category()`
Get::plug('category', function() { … });
Donation and Email Subscription

1 Comment

  1. Torsten


    I have always preferred categories. I’ve seen authors creating new tags for almost every new article, spoken exaggerating. And think of those nasty overloaded tag clouds, which were oh so hip several years ago. Nah… categories lead to a more structured thinking and working, in my opinion, because you wouldn’t create dozens or hundreds of categories… normally.

All comments are parsed with Parsedown Extra