https://github.com/jongacnik/kirby-props
Helper to convert an array to a Kirby content object
https://github.com/jongacnik/kirby-props
kirby kirby-plugin kirby3-plugin
Last synced: 11 months ago
JSON representation
Helper to convert an array to a Kirby content object
- Host: GitHub
- URL: https://github.com/jongacnik/kirby-props
- Owner: jongacnik
- License: mit
- Created: 2020-07-08T05:58:36.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-08T06:12:13.000Z (almost 6 years ago)
- Last Synced: 2025-04-02T15:38:21.617Z (about 1 year ago)
- Topics: kirby, kirby-plugin, kirby3-plugin
- Language: PHP
- Homepage:
- Size: 3.91 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Kirby Props → `kprops`
Helper to convert arrays to [`Kirby\Cms\Content`](https://getkirby.com/docs/reference/@/classes/cms/content) objects, particularly useful for snippets:
**Pass props**
```php
snippet('some-snippet', kprops([
'text' => 'Here *is* some **text**',
'image' => 'filename.jpg',
'page' => 'some/page'
]));
```
**Access in `some-snippet`**
```php
= $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`](https://getkirby.com/docs/reference/@/classes/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.
```php
$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:
```php
$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`:
```php
$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:
```php
snippet('some-snippet', [
'key' => 'value',
'props' => kprops([], page(), true)
]);
```
## Configuration
You can set the `key` content is assigned to:
**config.php**
```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
```