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

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

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 to Kirby\Cms\Content
  • Page $parent → parent Page object for the content object. Defaults to current page.
  • Boolean $raw → if true returns content object directly. Default false.

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
Other installation methods

Download

Download and copy this repository to /site/plugins/kirby-props.

Git submodule

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