jg/kirby-props

Kirby Props

安装次数: 10

依赖项: 0

建议者: 0

安全: 0

星标: 5

关注者: 1

分支: 0

公开问题: 0

类型:kirby-plugin

v1.0.0 2020-07-08 06:09 UTC

This package is auto-updated.

Last update: 2024-09-08 16:17:01 UTC


README

将数组转换为 Kirby\Cms\Content 对象的辅助工具,特别适用于代码片段

传递属性

snippet('some-snippet', kprops([
  'text' => 'Here *is* some **text**',
  'image' => 'filename.jpg',
  'page' => 'some/page'
]));

some-snippet 中访问

<?= $props->text()->kirbytext() ?>
<?= $props->image()->toFile()->url() ?>
<?= $props->page()->toPage()->url() ?>

用法

kprops($data, $parent, $raw)

  • Array $data *必需 → 要转换为 Kirby\Cms\Content 的数组
  • Page $parent → 内容对象父 Page 对象。默认为当前页面。
  • Boolean $raw → 如果 true 直接返回内容对象。默认 false

详细信息

在传递属于特定页面的值(如文件名)时使用 $parent 参数。

$data = [ 'image' => 'filename.jpg' ]; // <- let’s say this file belongs to `some/page`
$page = page('some/page');

snippet('some-snippet', kprops($data, $page));

kprops 返回一个看起来像这样的数组

$output = kprops([]); // => [ 'props' => Kirby\Cms\Content ]

这对于期望数组类型的代码片段来说很理想。不过,可以通过传递 $raw 参数直接返回 Content

$output = kprops([], page(), true); // => Kirby\Cms\Content

这在将 kprops 用于代码片段之外的环境或向代码片段传递额外值时非常有用

snippet('some-snippet', [
  'key' => 'value',
  'props' => kprops([], page(), true)
]);

配置

您可以设置内容分配到的 key

config.php

[
  'jg.kprops.key' => 'props' // default
]

安装

composer require jg/kirby-props
其他安装方法

下载

下载并将此存储库复制到 /site/plugins/kirby-props

Git 子模块

git submodule add https://github.com/jongacnik/kirby-props.git site/plugins/kirby-props