hananils/kirby-type-methods

Kirby 类型方法

安装: 28

依赖: 0

建议者: 0

安全性: 0

星标: 5

关注者: 4

分支: 0

开放问题: 0

类型:kirby-plugin

1.0.0 2021-11-17 11:31 UTC

This package is auto-updated.

Last update: 2024-09-06 16:28:53 UTC


README

Kirby Type Methods

类型方法 是一个为 Kirby 3 设计的插件,用于识别对象类型,无论是页面、用户还是文件,以便简化处理来自 $page$user 对象内容的代码片段中的逻辑。

示例

想象一个需要列出成员的网站。成员信息可能存储在 Kirby 用户中(供管理员使用)或页面上(供普通成员使用)。假设你有一个包含用户或页面对象的成员数组,类型方法将帮助你识别来源

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

还有快捷方式可以检查模板或角色

<?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; ?>

使用 isType() 比检查 intendedTemplaterole 更短,而且不需要你首先检查你正在处理的对象是页面还是用户。

安装

下载

下载并将此存储库复制到 /site/plugins/type-methods

Git 子模块

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

Composer

composer require hananils/kirby-type-methods

方法

这些方法适用于页面、用户或文件对象

isPage()

检查给定的对象是否为页面。

$object->isPage();

返回 truefalse

isUser()

检查给定的对象是否为用户。

$object->isUser();

返回 truefalse

isFile()

检查给定的对象是否为文件。

$object->isFile();

返回 truefalse

isType('template')

检查对象是否具有给定的模板。

$page->isType('article');

返回 truefalse

许可证

此插件由 MIT 许可证 提供,由 hana+nils · Büro für Gestaltung。我们为数字和模拟媒体创建视觉设计。