PHP code example of lukaskleinschmidt / kirby-snippet-controller

1. Go to this page and download the library: Download lukaskleinschmidt/kirby-snippet-controller library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lukaskleinschmidt / kirby-snippet-controller example snippets


// header.controller.php

// The return value can be a callback
return function ($site) {
    return [
        'title' => $site->title(),
    ];
};

// Or you can simply return an array.
return [
    'title' => site()->title(),
];


Kirby::plugin('superwoman/superplugin', [
    'snippets' => [

        // Refer to a file
        'header.controller' => __DIR__ . '/snippets/header.controller.php',

        // Return an array
        'header.controller' => [
            'title' => site()->title(),
        ],

        // Return a callback
        'header.controller' => function ($site) {
            return [
                'title' => $site->title(),
            ];
        },

    ],
]);

snippet('header', data: ['title' => 'My Title'])

// header.controller.php

return function (string $title = 'Default Title', ...$args) {
    return [
        'title' => $title,
    ];
};

snippet('header', data: ['size' => $page->size()])

// header.controller.php

return function (Field|string $size = null) {

    if ($size instanceof Field) {
        $size = $size->value();
    }

    $size = match ($size) {
        'small'  => 'height: 50vh',
        'medium' => 'height: 80vh',
        default  => 'height: 100vh',
    };

    return [
        'size' => $size,
    ];
};

snippet('header', data: ['size' => $page->size()])

// header.controller.php

return function (Field|string $size = null) {
    if (is_null($size)) {
        snippet_prevent();
    }

    return [
        'size' => $size,
    ];
};

// config.php

return [
    'lukaskleinschmidt.snippet-controller' => [

        // The default resolver
        'name' => fn (string $name) => $name . '.controller',

        // You might want to store controllers in a separate folder
        'name' => fn (string $name) => 'controllers/' . $name,

    ],
];

bash
📁 snippets
├─ 📄 header.controller.php
└─ 📄 header.php