jg / kirby-props
Kirby Props
Installs: 10
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 0
Open Issues: 0
Type:kirby-plugin
Requires
This package is auto-updated.
Last update: 2024-04-08 15:29:54 UTC
README
Helper to convert arrays to Kirby\Cms\Content
objects, particularly useful for snippets:
Pass props
snippet('some-snippet', kprops([ 'text' => 'Here *is* some **text**', 'image' => 'filename.jpg', 'page' => 'some/page' ]));
Access in some-snippet
<?= $props->text()->kirbytext() ?> <?= $props->image()->toFile()->url() ?> <?= $props->page()->toPage()->url() ?>
Usage
kprops($data, $parent, $raw)
Array $data
*required → array to convert toKirby\Cms\Content
Page $parent
→ parentPage
object for the content object. Defaults to current page.Boolean $raw
→ iftrue
returns content object directly. Defaultfalse
.
Details
Use the $parent
argument when passing values (such as a filename), that belong to a specific page.
$data = [ 'image' => 'filename.jpg' ]; // <- let’s say this file belongs to `some/page` $page = page('some/page'); snippet('some-snippet', kprops($data, $page));
kprops
returns an array which looks like:
$output = kprops([]); // => [ 'props' => Kirby\Cms\Content ]
This is ideal for snippets which expect arrays. That said, the $raw
argument can be passed to directly return the Content
:
$output = kprops([], page(), true); // => Kirby\Cms\Content
This is useful when using kprops
outside of the context of snippets, or, if passing additional values into a snippet:
snippet('some-snippet', [ 'key' => 'value', 'props' => kprops([], page(), true) ]);
Configuration
You can set the key
content is assigned to:
config.php
[ 'jg.kprops.key' => 'props' // default ]
Installation
composer require jg/kirby-props