hananils/kirby-link-methods

将 Kirby 对象转换为逗号分隔的自然列表的方法

安装: 29

依赖: 0

建议者: 0

安全: 0

星标: 9

关注者: 5

分支: 0

开放问题: 0

类型:kirby 插件

2.2.0 2023-02-16 10:07 UTC

This package is auto-updated.

Last update: 2024-09-16 15:15:15 UTC


README

Kirby List Methods

列表方法 是一个为 Kirby 3 提供从页面、用户或文件等集合生成逗号分隔列表的插件的插件。它允许自定义列表值字段选择,可选的连接词用于最后一个项目(例如“和”),以及每个项目可以定义的自定义链接,这些链接可以使用 Kirby 的查询语言(例如 {{page.url}})来定义。它还提供了列出数值(如年份)、缩短范围以增强可读性的特定方法。

安装

下载

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

Git 子模块

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

Composer

composer require hananils/kirby-list-methods

页面、用户和文件方法

有不同选项可以创建用户、页面或文件集合的列表

使用主字段创建逗号分隔列表

创建一个使用逗号分隔所有项目的列表

// using the username
$users->toList();

// using the page title
$pages->toList();

// using the filename
$files->toList();

创建一个年份列表

// numeric list, returns "2010–2012, 2022" for "2010, 2011, 2012, 2022"
$page->years()->toNumericList();

// numeric list with "since", return "since 2020" for "2020, 2021, 2022"
$page->years()->toNumericList(true);

使用自定义字段或方法创建逗号分隔列表

创建一个使用逗号分隔所有项目的列表

// using the custom method `nickname`
$users->toList('nickname');

// using the field category
$pages->toList('category');

// with numeric values
$pages->toNumericList('date');

使用连接词创建逗号分隔列表

创建一个使用逗号分隔所有项目但最后一个与连接词相连的列表

// creates, a, list, with, commas and conjunction
$pages->toList('title', true)

// creates, a, list, with, commas & conjunction
$pages->toList('title', '&')

默认连接词 and 依据您的语言设置提供英文或德文。

具有动态链接的逗号分隔列表

创建一个链接到自定义目的地的列表

// link everything to the same URL
$pages->toList('title', true, 'https://example.com');

// link all pages to their own URL
$pages->toList('title', true, '{{page.url}}');

// link all pages to a custom URL
$pages->toList('title', true, 'my-custom-path/{{page.category}}');

// link all pages to a custom URL with numeric values
$pages->toNumericList('date', true, 'my-year-overview/{{page.date.toDate('Y')}}');

您可以使用 Kirby 的模板语法和 查询语言 来使用模板语法,从当前上下文中获取任何信息,例如当前 $user$page$file 对象。也可以使用 $kirby$site 对象。

内容方法

当处理单个页面或用户时,有方法可以生成内容字段列表

// Given the fields name and job, creates "Jane Doe, astrophysicist"
echo $user->asList(['name', 'job']);

// Given the fields start and end, creates "2020–2023"
echo $page->asNumericList(['start', 'end']);

两种方法,asListasNumericList,都支持通过次级属性设置自定义连接词

//  Given the fields name and job, creates "Jane Doe: astrophysicist"
echo $page->asList(['name', 'job'], ': ');

集合方法

此插件还提供了一个更简单的通用集合方法,它是 naturalList() 辅助程序的快捷方式,并且只允许自定义连接词

// Create a Choices collection for instance, see https://github.com/hananils/kirby-choices
$choices = $page->categories()->toChoices();

// Convert all choices to a comma-separated list
echo $choices->toList();

// Convert all choices to a comma-separated list with the default conjunction
echo $choices->toList(true);

// Convert all choices to a comma-separated list with a custom conjunction
echo $choices->toList('&');

辅助程序

如果您想在 Kirby 对象之外创建列表(例如从数组中),可以使用 naturalList() 辅助程序。它接受一个平面数组和一个连接词,没有自定义键选择或模板语法支持

$data = ['this', 'that'];

// this, that
naturalList($data);

// this and that
naturalList($data, true);

// this & that
naturalList($data, '&');

如果您处理数值,可以使用 numericList() 辅助程序

$data = [2019, 2020, 2021, 2022];

// 2019-2022
numericList($data);

// since 2022
numericList($data, true);

许可

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