Mecha CMS

Add extra functionality to the core engine.

Tag

Table of Contents
  1. Working with Tags 
    1. Page Properties 
    2. File Structure 
    3. File Content 
    4. Connections 
  2. Variables 
  3. Conditions 
  4. Classes 

List all pages in the current folder filtered by tag.

Download Stable Version Download Development Version

Something looks broken? We accept bounties to fix them faster.

^243 Downloads
v1.11.262 Downloads
v1.11.136 Downloads
v1.10.247 Downloads
v1.10.166 Downloads
v1.9.645 Downloads
v1.9.540 Downloads
v1.9.439 Downloads
v1.9.370 Downloads
v1.9.237 Downloads
v1.5.057 Downloads

This extension activates the tag feature by utilizing the kind property of the page. This extension will also add several new routes such as https://mecha-cms.com/blog/tag/:tag on every page to allow users to list all pages in the current folder by a tag.

Working with Tags 

Panel extension can help you to automate everything. But, if you have to, you can still make this extension work even without the GUI feature. Before we start, you need to know how this extension works.

Page Properties 

Get to know that this extension adds query and tags properties to the page only if the page contains kind property that is not empty:

$page = Page::__set_state([
    …
    …
    'kind' => [1, 2, 3], // [^1]
    'query' => ['foo', 'bar', 'baz'], // [^2]
    'tags' => Tags::__set_state([ // [^3]
        'foo' => Tag::__set_state([
            …
            …
            'id' => 1,
            'link' => 'https://mecha-cms.com/tag/foo/1',
            'name' => 'foo'
        ]),
        'bar' => Tag::__set_state([
            …
            …
            'id' => 2,
            'link' => 'https://mecha-cms.com/tag/bar/1',
            'name' => 'bar'
        ]),
        'baz' => Tag::__set_state([
            …
            …
            'id' => 3,
            'link' => 'https://mecha-cms.com/tag/baz/1',
            'name' => 'baz'
        ])
    ])
]);

// [^1]: These numbers will be used as the tag ID.
// [^2]: These queries are generated automatically based on the available tags.
// [^3]: These rich tags data are also generated based on the available tags.

File Structure 

Writing a tag file is the same as writing a page file. The only difference is that you must write the id property separately from the page:

.\
└── lot\
    └── tag\
        ├── bar\
        │   └── id.data
        ├── baz\
        │   └── id.data
        ├── foo\
        │   └── id.data
        ├── bar.page
        ├── baz.page
        └── foo.page

File Content 

Make sure to specify a unique number for each id.data file. These numbers, then will be used to connect the tag file with the kind property on every page.

Here’s an example of a tag file content:

---
title: Tag Name
description: Short description about this tag.
...

Long description about this tag.

Connections 

To connect between tags with the current page, create a kind.data file in the corresponding folder, containing a list of tag’s ID written in a valid JSON format:

.\
└── lot\
    └── page\
        ├── test-page\
        │   └── kind.data
        └── test-page.page

Example content of kind.data file:

[1, 2, 3]

You can actually insert the kind property in the page header, but this method is a little inefficient in terms of performance:

---
title: Page Title
description: Page description.
author: Taufik Nurrohman
type: Markdown
kind: [1, 2, 3]
...

Page content goes here.

Variables 

A global variable $tag will be available in tags page mode. This variable is an instance of the Tag class which you can use to get the current tag details.

if ($site->is('tags')) {
    echo i('Current tag is %s.', [$tag->title]);
}

Conditions 

These page conditional statements are available:

  • $site->is('tags') → Return true if current items view is a tags view and is not empty.

Classes 

These HTML classes will be added to the <html> element that has a class attribute:

  • is:tags → Will be added if current items view is a tags view and is not empty.