hananils/kirby-type-methods

Kirby Type Methods

1.0.0 2021-11-17 11:31 UTC

This package is auto-updated.

Last update: 2024-11-06 16:47:30 UTC


README

Kirby Type Methods

Type Methods is a plugin for Kirby 3 helping to identify the type of an object, either page, user or file, in order to simplify logic in snippets that – for instance – handle content from both $page and $user objects.

Examples

Think of site where you want to list members. Member information is either stored with the Kirby users (for admins) or on pages (for regular members). Given you have an array of all members, containing either user or page objects, type methods will help you to identify the source:

<?php foreach ($members as $member): ?>
  <?php if ($member->isUser()): ?>
    <?= $member->username() ?> (<?= $member->role() ?>)
  <?php else: ?>
    <?= $member->title() ?>
  <?php endif; ?>
<?php endforeach; ?>

There is also a shortcut to check the given template or role respectively:

<?php if ($member->isType('parents')): ?>
  <!-- do stuff specific to all pages with the `intendedTemplate` of `parents` -->
<?php elseif ($member->isType('admin')): ?>
  <!-- do stuff specific to all users with the `role` of `admin` -->
<?php endif; ?>

Using isType() is both shorter than checking intendedTemplate or role and also doesn't require you to check first which object you are dealing with (page or user).

Installation

Download

Download and copy this repository to /site/plugins/type-methods.

Git submodule

git submodule add https://github.com/hananils/kirby-type-methods.git site/plugins/type-methods

Composer

composer require hananils/kirby-type-methods

Methods

These methods work on page, user or file objects:

isPage()

Checks if the given object is a page.

$object->isPage();

Returns either true or false.

isUser()

Checks if the given object is a user.

$object->isUser();

Returns either true or false.

isFile()

Checks if the given object is a file.

$object->isFile();

Returns either true or false.

isType('template')

Checks if the object has the given template.

$page->isType('article');

Returns either true or false.

License

This plugin is provided freely under the MIT license by hana+nils · Büro für Gestaltung. We create visual designs for digital and analog media.