Search by

beebmx / kirby-blade

beebmx

Blade template for Kirby

Package info

github.com/beebmx/kirby-blade

Type:kirby-plugin

pkg:composer/beebmx/kirby-blade

Fund package maintenance!

beebmx

Statistics

Installs: 3 033

Dependents: 1

Suggesters: 0

Stars: 14

Open Issues: 0

7.2.0 2026-11-29 21:38 UTC

README

Kirby Blade use Laravel illuminate/view and beebmx/blade packages.

This package enables Laravel Blade for your own Kirby applications.

Requirements

  • PHP 8.4 or higher
  • Kirby 4, 5 or 6
  • illuminate/view 12 or 13

The PHP versions you can use depend on your Kirby version:

Kirby PHP 8.4 PHP 8.5 PHP 8.6
Kirby 4 ✅ ❌ ❌
Kirby 5 ✅ ✅ ❌
Kirby 6 ✅ ✅ ✅

Note

On PHP 8.6, use illuminate/view 13. Version 12 works, but triggers spl_object_hash() deprecation notices.

Version support

Version Kirby 3 Kirby 4 Kirby 5 Kirby 6 PHP
7.x ❌ ✅ ✅ ✅ 8.4+
6.1 – 6.2 ❌ ✅ ✅ ❌ 8.2+
6.0 ❌ ✅ ❌ ❌ 8.2+
5.x ❌ ✅ ❌ ❌ 8.1+
4.x ✅ ❌ ❌ ❌ 8.0+
1.x – 3.x ✅ ❌ ❌ ❌ 7.1+

Installation

Installation with composer

composer require beebmx/kirby-blade

Upgrading

Upgrading from 6.x? Check the upgrade guide.

What is Blade?

According to Laravel Blade documentation is:

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

Usage

You can use the power of Blade like Layouts, Sub-Views, Directives, your Custom If Statements and Blade components.

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

Options

The default values of the package are:

Option Default Values Description
beebmx.kirby-blade.bootstrap true (bool) Enable blade facade after the plugin is available
beebmx.kirby-blade.views site/cache/views (string) Location of the views cached
beebmx.kirby-blade.directives [] (array) Array with the custom directives
beebmx.kirby-blade.ifs [] (array) Array with the custom if statements
beebmx.kirby-blade.blaze true (bool) Compile the components marked with @blaze

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

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:

'beebmx.kirby-blade.views' => '/site/storage/views',

Directives

Kirby Blade comes with the following directives. They call Kirby's classes directly (Kirby\Cms\Html, Kirby\Toolkit\I18n, ...) instead of Kirby's global helpers, so they keep working even if you disable a helper.

Text

Directive Description
@kirbytext($text), @kt($text) Renders KirbyText and Markdown
@kirbytextinline($text), @kti($text) Renders KirbyText and Markdown without the <p> wrapper
@markdown($text) Renders Markdown
@smartypants($text) Applies SmartyPants typography
@esc($string) Escapes a string for HTML (Str::esc())
@html($text), @h($text) Encodes HTML entities (Html::encode())

Translations

Directive Description
@translate($key, $fallback), @t($key, $fallback) Prints a translation
@lang($key, $fallback) Alias of @t
@tc($key, $count) Prints a translation for a count, like {{ count }} apples
@choice($key, $count) Alias of @tc
@tt($key, $replace) Prints a translation with placeholders, like Hello {name}

URLs, assets and HTML

Directive Description
@url($path), @u($path) Prints the absolute URL for a path
@asset($path) Prints the URL of a file in your site, like assets/logo.svg
@css($url, $options) Renders a <link rel="stylesheet"> tag
@js($url, $options) Renders a <script> tag
@vite($entrypoints, $buildDirectory) Renders the Vite tags from the manifest, or from the dev server
@viteReactRefresh Renders the React refresh script while the Vite dev server runs
@fonts($aliases) Renders the font preloads and styles from the Vite fonts option
@image($path) Renders an <img> tag, for the first image of the page if no path is given
@svg($file) Renders the content of an SVG file inline
@attr($attributes) Renders HTML attributes, like @attr(['data-id' => 7, 'hidden' => true])
@video($url, $options, $attr) Embeds a YouTube or Vimeo video, or a video file
@youtube($url, $options, $attr) Embeds a YouTube video
@vimeo($url, $options, $attr) Embeds a Vimeo video
@gist($url, $file) Embeds a GitHub gist, or a single file of it

Pages and snippets

Directive Description
@page($id) Prints the id of a page, or of the current page if no id is given
@pages($ids) Prints the ids of several pages
@snippet($name, $data) Renders a PHP or Blade snippet, see Snippets
@snippet($name, $data, slots: true) ... @endsnippet Renders a snippet with slots, see Snippets

Others

Directive Description
@csrf() Prints Kirby's CSRF token
@dump($variable) Dumps a variable
@go($url, $code) Redirects to the URL, with a 302 status code by default

But you can create your own:

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

If Statements

Kirby Blade comes with these if statements:

@auth
    Welcome back {{ $kirby->user()->name() }}
@endauth

@guest
    Please Log In
@endguest

Like directives, you can create your own if statements:

'beebmx.kirby-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

Snippets

Blade snippets are regular Kirby snippets with the .blade.php extension. Place them in site/snippets, and they can use everything Blade offers:

<!-- ../snippets/header.blade.php -->

<header>
    <h1>{{ $title ?? $page->title() }}</h1>
    <nav>
        @foreach ($site->children()->listed() as $item)
            <a href="{{ $item->url() }}" @class(['active' => $item->isOpen()])>{{ $item->title() }}</a>
        @endforeach
    </nav>
</header>

Like any Kirby snippet, they receive $kirby, $site and $page, plus the data you pass to them.

Rendering snippets

Render a snippet from a Blade template with the @snippet directive, and from a PHP template with Kirby's snippet() helper. Blade and PHP snippets can be mixed in both kinds of templates:

<!-- ../templates/default.blade.php -->

@snippet('header', ['title' => 'Welcome'])

<!-- ../templates/default.php -->

<?php snippet('header', ['title' => 'Welcome']) ?>

Snippets in subfolders use the path of the folder, and a list of names renders the first snippet found:

@snippet('blocks/image', ['block' => $block])

@snippet(['articles/' . $page->intendedTemplate(), 'articles/default'])

To get the HTML as a string instead of printing it, use $kirby->snippet():

@php($header = $kirby->snippet('header', ['title' => 'Welcome']))

Since the site/snippets directory is also a Blade view location, you can include a Blade snippet with @include('header') too. In that case it's a regular Blade view: it receives the variables of the parent view, not Kirby's snippet data.

Note

If a snippet exists both as header.php and header.blade.php, the Blade snippet is rendered. A snippet and a template with the same name don't conflict: @snippet('card') always renders site/snippets/card.blade.php.

Snippets with slots

Blade snippets support slots, from Blade and PHP templates:

<!-- ../snippets/card.blade.php -->

<article class="card">
    <h2>{{ $title }}</h2>
    {!! $slot !!}
    @if ($slots->footer)
        <footer>{!! $slots->footer !!}</footer>
    @endif
</article>

<!-- ../templates/default.blade.php -->

@snippet('card', ['title' => $page->title()], slots: true)
    <p>The default slot</p>
@endsnippet

<!-- ../templates/default.php -->

<?php snippet('card', ['title' => $page->title()], slots: true) ?>
    <?php slot() ?><p>The default slot</p><?php endslot() ?>
    <?php slot('footer') ?>The footer slot<?php endslot() ?>
<?php endsnippet() ?>

Note

Kirby's slots contain HTML, so print them with {!! $slot !!}; {{ $slot }} escapes them. Blade's own @slot directive belongs to Blade components, so named slots in Blade templates use Kirby's slot() and endslot() helpers.

Snippets from plugins

Plugins can register Blade snippets like any other snippet, and they are rendered with Blade:

Kirby::plugin('acme/cards', [
    'snippets' => [
        'cards/product' => __DIR__ . '/snippets/product.blade.php',
    ],
]);

Components

Now you can use Blade components natively in Kirby 3. To display a component its required to place your component in templates/components and then you can call it with the prefix x- in kebab case.

<!-- ../templates/components/alert.blade.php -->

<x-alert/>


<!-- ../templates/components/button.blade.php -->

<x-button></x-button>

If your component is nested deeper inside the components directory, you can use the . character to indicate the place:

<!-- ../templates/components/inputs/button.blade.php -->

<x-inputs.button/>

You can also send data to the components via "slots" and attributes:

<x-alert title="Danger">Message</x-alert>

<!-- ../templates/components/alert.blade.php -->

<div class="alert">
    <div>{{$title}}</div>
    <div>{{ $slot }}</div
</div>

All the documentation related to Components is in the Laravel website.

Vite

Kirby Blade works with laravel-vite-plugin, including its fonts option:

import { defineConfig } from 'vite'
import laravel, { refreshPaths } from 'laravel-vite-plugin'
import { bunny } from 'laravel-vite-plugin/fonts'
import tailwindcss from '@tailwindcss/vite'

export default defineConfig({
  plugins: [
    tailwindcss(),
    laravel({
      input: ['resources/css/app.css', 'resources/js/app.js'],
      refresh: [...refreshPaths, 'site/templates/**'],
      fonts: [
        bunny('Inter', {
          weights: [400, 500, 600],
        }),
      ],
    }),
  ],
  resolve: {
    alias: {
      '@': '/resources/js',
    },
  },
})

Then render the fonts and the assets in the <head> of your layout:

<head>
    @fonts
    @vite(['resources/css/app.css', 'resources/js/app.js'])
</head>

@fonts renders the preload tags and the @font-face rules of every font. Each font gets an alias and a CSS variable from its name (inter and --font-inter), so you can render only some of them with @fonts('inter') or @fonts(['inter', 'mono']). In development, both directives use the Vite dev server while its hot file exists.

Blaze

Kirby Blade comes with Blaze, which compiles anonymous components into PHP functions and skips most of Blade's rendering overhead. Add the @blaze directive at the top of a component to optimize it:

<!-- ../templates/components/button.blade.php -->

@blaze

@props(['type' => 'button'])

<button {{ $attributes->merge(['type' => $type]) }}>
    {{ $slot }}
</button>

The component is used the same way, <x-button type="submit">Send</x-button>, and renders the same HTML. Components without @blaze are still rendered by Blade.

Blaze's strategies are passed as parameters of the directive:

@blaze(memo: true) // Renders the component once for the same props, only for components without slots

@blaze(fold: true) // Renders the component into static HTML when the template is compiled

A component with fold: true is only folded when its props are static. You can tune this with safe and unsafe:

Parameter Type Default Description
fold (bool) false Enable compile-time folding
memo (bool) false Enable runtime memoization
safe (array) [] Props that may fold even when dynamic
unsafe (array) [] Props that should abort folding when dynamic
@blaze(fold: true, safe: ['level']) // Folds even when level is dynamic, like :level="$level"

@blaze(fold: true, unsafe: ['slot']) // Doesn't fold when the component has content in its slot

safe and unsafe also accept slot names, attributes for the attributes that aren't props, and * for all of them.

Read the Blaze documentation before using fold, since a folded component that depends on runtime data renders stale HTML.

Blaze has some limitations, so a component marked with @blaze:

  • Can't be a class component
  • Doesn't have the $component variable
  • Doesn't trigger view composers or creators
  • Shares @aware values only with parents marked with @blaze
  • Can't be rendered with view(), only with its <x-...> tag

Blaze is enabled by default. You can disable it in your config.php, and the components marked with @blaze are rendered by Blade:

'beebmx.kirby-blade.blaze' => false,

Views compiled with and without Blaze are stored apart, so you can turn Blaze on and off without clearing them.

Important

After adding or removing @blaze in a component that is already used, clear the compiled views with kirby view:clear. Until the templates that use the component are compiled again, it renders nothing (when added) or fails (when removed).

Blaze's directory optimization (Blaze::optimize()) and its debugger are not supported.

Conflicts

Kirby declares some global helpers whose names clash with helpers from Laravel and other packages. The most common one is e(), which Laravel's illuminate/support package declares too, which ends with a Cannot redeclare function e() error.

When Kirby Blade is installed with Composer, it disables Kirby's e(), dump() and go() helpers for you by defining these constants:

KIRBY_HELPER_E = false
KIRBY_HELPER_DUMP = false
KIRBY_HELPER_GO = false

As a result:

  • e() is Laravel's helper, which escapes HTML and returns the result instead of echoing it. Blade's {{ }} echoes don't depend on it.
  • dump() and go() aren't available unless another package declares them, for example symfony/var-dumper for dump(). Otherwise, use the Kirby classes behind them:
use Kirby\Cms\Helpers;
use Kirby\Cms\Response;

Helpers::dump($variable); // instead of dump($variable)

Response::go('/blog'); // instead of go('/blog')

Laravel helpers

Blaze calls Laravel's app() and config() helpers, so Kirby Blade declares them when they don't exist. app() returns Kirby Blade's container, and config() its configuration. Since PHP function names are case-insensitive, a site that declares its own app() or App() function ends with a Cannot redeclare function app() error. Rename that function, or declare it inside function_exists().

Defining the constants yourself

Kirby Blade only defines a constant that isn't defined yet, so you can still define them yourself. You need to when Kirby Blade isn't loaded through Composer's autoloader, or when Composer loads Kirby's helpers before Kirby Blade's. Define them in the index.php file of your public directory, before including autoload.php:

<?php

const KIRBY_HELPER_E = false;
const KIRBY_HELPER_DUMP = false;
const KIRBY_HELPER_GO = false;

include '../vendor/autoload.php';

// ...

To keep one of Kirby's helpers, define its constant as true instead, for example const KIRBY_HELPER_GO = true;. Don't do this for e(), because Laravel's e() would clash with it.