Article · Mecha CMS

Updates.

Page Conditional Statement Concept

Author: Taufik Nurrohman · 1191 Views

Tweet

Making a better page conditional mechanism.

I am currently working on a maintainable and extensible page conditional statement mechanism. Currently, we use the is property that is stored in the $site variable to indicate what type of page we are currently in:

if ($site->is === 'page') {
    // A single page.
}

The problem here is that the is property can only store single state of the page condition, which will cause some problems if, for example, we want to check whether we are in a list of pages filtered by tag:

if (
    $site->is === 'pages' &&
    strpos($url->path . '/', Extend::state('tag', 'path') . '/') === 0
) {
    // A tags page.
}

I want to make something that is much simpler than setting up condition by detecting the current URL path manually. I want to do something like this:

if ($site->is('pages') && $site->is('tag')) {
    // A tags page.
}

Or even simpler:

if ($site->any(['tag', 'search'])) {
    // A tags page or search page.
}

So that calling an undetermined condition mechanism will not trigger any error messages and simply return a false or null value. What do you think?


Other alternatives:

if (Is::pages() && Is::tag()) { … }
if (is('pages') && is('tag')) { … }
function is() { … }
function not() { … }
function any() { … }
function has() { … }

3033332000

1 Comment