PHP code example of craftile / laravel

1. Go to this page and download the library: Download craftile/laravel 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/ */

    

craftile / laravel example snippets




namespace App\Blocks;

use Craftile\Core\Contracts\BlockInterface;
use Craftile\Core\Data\Property\Text;
use Craftile\Core\Data\Property\Boolean;

class Hero implements BlockInterface
{
    public static function getType(): string
    {
        return 'hero';
    }

    public static function getLabel(): string
    {
        return 'Hero Section';
    }

    public static function properties(): array
    {
        return [
            Text::make('title', 'Title')->default('Welcome'),
            Text::make('subtitle', 'Subtitle'),
            Boolean::make('showButton', 'Show Button')->default(true),
        ];
    }

    protected static array $accepts = ['button', 'text']; // Optional: specify accepted child types

    public function render(): mixed
    {
        return view('blocks.hero');
    }
}

// In AppServiceProvider or dedicated BlockServiceProvider
use Craftile\Laravel\Facades\Craftile;

public function boot()
{
    Craftile::registerBlock(Hero::class);
}

Route::get('/', function () {
    return view('pages.homepage'); // Will automatically compile the JSON template
});

use Craftile\Core\Data\Property\{Text, Textarea, Boolean, Select};

public static function properties(): array
{
    return [
        Text::make('title', 'Title')
            ->default('Default title')
            ->          ->options([
                ['value' => 'sm', 'label' => 'Small'],
                ['value' => 'md', 'label' => 'Medium'],
                ['value' => 'lg', 'label' => 'Large'],
            ])
            ->default('md'),
    ];
}

// In your block class
protected static array $accepts = ['*']; // Accept any child type
// or
protected static array $accepts = ['button', 'text']; // Accept specific types

// In your block template
@children {{-- Renders all child blocks --}}

'directives' => [
    'craftileBlock' => 'block',        // @block instead of @craftileBlock
    'craftileContent' => 'content',    // @content instead of @craftileContent
],

'components' => [
    'namespace' => 'builder', // <builder:block> instead of <craftile:block>
],
bash
php artisan vendor:publish --provider="Craftile\Laravel\CraftileServiceProvider"
blade
<!-- resources/views/blocks/hero.blade.php -->
<section class="hero bg-gray-900 text-white py-16">
    <div class="container mx-auto px-4">
        <h1 class="text-4xl font-bold mb-4">{{ $block->properties->title }}</h1>

        @if($block->properties->subtitle)
            <p class="text-xl mb-8">{{ $block->properties->subtitle }}</p>
        @endif

        @if($block->properties->showButton)
            @children
        @endif
    </div>
</section>