Skip to content

hananils/kirby-type-methods

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 

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.

About

Kirby 3 plugin providing methods to check the type of a Kirby object.

Topics

Resources

License

Stars

Watchers

Forks

Languages