seymennc/php-blade

There is no license information available for the latest version (dev-master) of this package.

dev-master 2024-09-22 20:16 UTC

This package is auto-updated.

Last update: 2025-04-22 21:33:03 UTC


README

Luminance PHPBlade is a lightweight templating engine built in PHP, inspired by Laravel's Blade templating system. It allows you to separate your application's logic from the presentation layer using clear, easy-to-use syntax.

Badges

Latest Stable Version

Total Downloads

Latest Unstable Version

License

PHP Version Require

Version

🔗 Links

portfolio linkedin twitter

Features

  • Lightweight: Minimal and easy to integrate into any PHP project.
  • Dynamic Templating: Define sections, layouts, and reusable components
  • CSRF Protection: Built-in support for including CSRF tokens in your forms.
  • View Caching: Cache compiled views for improved performance.
  • Blade-style Syntax: Utilizes similar directives as Laravel's Blade for a familiar experience.

Installation

To use the project, follow these steps:

Git Clone

git clone https://github.com/seymennc/php-blade.git
cd php-blade
php -S 127.0.0.1:8080

Composer

If you create new project with php-blade

composer create-project seymennc/php-blade php-blade
cd php-blade
php -S 127.0.0.1:8080

If you will only use it in your project

composer require seymennc/php-blade

Usage

Include the autoloader in your project:

require 'vendor/autoload.php';

Make sure to define the paths for the view and cache directories in your configuration:

// Example configuration in config.php
return [
    [
        'views' => dirname(__DIR__) . '/resources/views',
        'cache' => dirname(__DIR__) . '/storage/cache',
        'suffix' => '.blade.php'
    ]
];

Rendering a View

To render a view, simply call the Blade::view() method:

use Luminance\Service\phpblade\Blade\Blade;

echo Blade::view('home');

Or you can use it like this

use Luminance\Service\phpblade\Blade\Blade;

view('home');

Should you need to pass data to the view, you can do so by passing an array as the second argument:

echo Blade::view('home', ['title' => 'Welcome Home']);
//Or use this
view('home', ['title' => 'Welcome Home']);

Sections and Extending Layouts

You can define sections and extend layouts using the familiar Blade-style directives:

// layouts/master.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>@yield('title')</title>
</head>
<body>
    @yield('content')
</body>
</html>

// views/home.blade.php
@extends('layouts.master')

@section('title', 'Home Page')

@section('content')
<h1>Welcome to the Home Page</h1>
@endsection

CSRF Tokens

For forms that require CSRF tokens:

<form method="POST" action="/submit">
    @csrf
    <!-- form fields -->
</form>

All Usable Directives

@include('view')
{{ /* For PHP Variables */}}
@foreach(array)
    // Loop
@endforeach
@section('section')
    // Section
@endsection
@extends('layout')
@yield('section')
@csrf

More examples will be found in the Asgard Docs.

Contributing

We welcome contributions! If you find a bug or have suggestions for improvements, please open an issue or contribute directly to the project.

Our ♥️ Contributors

License

Licensed under the GNU GENERAL PUBLIC LICENSE, Copyright © 2024-present BLC Studio