leitsch/kirby-blade

Enable Laravel Blade Template Engine for Kirby 4

Installs: 2 328

Dependents: 0

Suggesters: 0

Security: 0

Stars: 17

Watchers: 3

Forks: 5

Open Issues: 3

Type:kirby-plugin

3.0.2 2024-04-12 08:50 UTC

README

Source Download Open Issues Last Commit Release License

Kirby Blade use Laravel illuminate/view 11.x package and compatible with Kirby 4.

This package enables Laravel Blade for your own Kirby applications.

Installation

composer require leitsch/kirby-blade

Caveat: Laravel and Kirby both define the e() helper function, but they do vastly different things. In Kirby, e() is basically just a shortcut for echo $condition ? $a : $b;. In Laravel, this function escapes HTML characters in a string. From Kirby 3.7 and up, you have to disable Kirby’s own e() helper by adding a single line of code to your index.php, before including the autoload.php file:

define('KIRBY_HELPER_E', false);

What is Blade?

According to Laravel Blade documentation is:

Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates. In fact, all Blade templates are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade template files use the .blade.php file extension.

Usage

You can use the power of Blade like Layouts, Forms, Sub-Views, Components, Directives and your custom if statements.

All the documentation about Laravel Blade is in the official documentation.

Options

The default values of the package are:

Option Default Values Description
leitsch.blade.templates site/templates (string) Location of the templates
leitsch.blade.views site/cache/views (string) Location of the views cached
leitsch.blade.directives [] (array) Array with the custom directives
leitsch.blade.ifs [] (array) Array with the custom if statements

All the values can be updated in the config.php file.

Templates

Default templates folder is site/templates directory or wherever you define your templates directory, but you can change this easily:

'leitsch.blade.templates' => '/theme/default/templates',

Views

All the views generated are stored in site/cache/views directory or wherever you define your cache directory, but you can change this easily:

'leitsch.blade.views' => '/site/storage/views',

Directives

By default, Kirby Blade comes with following directives:

@asset($path)
@csrf()
@css($path)
@dump($variable)
@e($condition, $value, $alternative)
@get($key, $default)
@gist($url)
@go($url, $code)
@h($string, $keepTags)
@html($string, $keepTags)
@js($path)
@image($path, $attr) // @image('forrest.jpg', 'url')
@kirbytag($type, $value, $attr)
@kirbytags($text, $data)
@kirbytext($text, $data)
@kirbytextinline($text)
@kt($text)
@markdown($text)
@option($key, $default)
@page($key, $attr) // @page('blog', 'title')
@param($key, $fallback)
@site($attr) // @site(title')
@size($value)
@smartypants($text)
@snippet($name, $data)
@svg($file)
@t($key, $fallback)
@tc($key, $count)
@tt($key, $fallback, $replace, $locale)
@u($path, $options)
@url($path, $options)
@video($url, $options, $attr)
@vimeo($url, $options, $attr)
@widont($string)
@youtube($url, $options, $attr)

But you can create your own:

'leitsch.blade.directives' => [
    'greeting' => function ($text)
    {
        return "<?php echo 'Hello: ' . $text ?>";
    },
],

Kirby Helpers Documentation:

https://getkirby.com/docs/reference/templates/helpers

If Statements

Like directives, you can create your own if statements:

'leitsch.blade.ifs' => [
    'logged' => function ()
    {
        return !!kirby()->user();
    },
],

After declaration, you can use it like:

@logged
    Welcome back {{ $kirby->user()->name() }}
@else
    Please Log In
@endlogged

Hook

For use cases such as HTML minification, there's a custom hook for manipulating rendered HTML output:

# site/config/config.php

# For this example, we are using 'voku/html-min'
use voku\helper\HtmlMin;

return [
    # ...

    'hooks' => [
        'blade.render:after' => function (string $html): string {
            return (new HtmlMin())->minify($html);
        },
    ],

    # ...
];

Credits