andrexm / astatine
A simple PHP template system inspired by Blade.
Requires
- php: >=8.0
README
A very simple and lightweight PHP template engine with a syntax inspired by Laravel's Blade. It's designed to be easy to use and integrate into your PHP projects without unnecessary complexity.
β¨ Features
-
Clean Syntax: Uses
@directives and{{ }}echo statements, familiar to Laravel developers. -
Template Inheritance: Supports
@extends,@section, and@yieldfor building layouts. -
Includes: Reuse template partials with the
@includedirective. -
Control Structures: Provides convenient directives like
@if,@else,@foreach,@while, etc. -
Echoing Data: Automatically escapes output with
{{ $variable }}for security. Use{!! $variable !!}for unescaped data. -
Lightweight & Fast: Compiles templates into plain PHP code for optimal performance.
π Installation
You can install the package via Composer:
composer require andrexm/astatine
π Basic Usage
Here's a quick example of how to use Astatine.
Configuration
First, you need to set up the engine with paths to your template directories.
<?php require 'vendor/autoload.php'; use Astatine\Engine; $engine = Engine::getInstance(); $engine::config( "views", // Directory for your template files "cache/views", // Directory for your compiled files (must be writable) ".php" // OPTIONAL: by default, the engine will look for .blade.php files ); // Render a template $engine::render('index', ['name' => 'John Doe']);
See examples/basics.php file. Also, as mentioned in the example above, the engine will look
for .blade.php files, in order to allow you to use extensions for syntax highlighting in Blade. If
you want to change that, just set the $extension to ".php" (as in the example above) or
other stuff.
Also, since you should provide a cache directory, you have to first create that directory:
mkdir -p cache/views
Example Template (templates/index.at.php)
Here is an example to show how similar it is to Blade. Actually, there is a single difference you
should be aware of: commands that define blocks must end with a :, like @if(...): ... @endif.
Follow the example below to understand better.
{{-- Template Inheritance Example --}}
@extends('layouts.master')
@section('title', 'Home Page')
@section('content'):
<h1>Hello, {{ $name }}!</h1>
@if (count($items) > 0):
<ul>
@foreach ($items as $item):
<li>{{ $item }}</li>
@endforeach
</ul>
@else:
<p>No items found.</p>
@endif
@include('partials.footer')
@endsection
Example Layout (templates/layouts/master.at.php)
<!DOCTYPE html> <html> <head> <title>@yield('title')</title> </head> <body> <header> {{-- Site header --}} </header> <main> @yield('content') </main> <footer> @yield('footer') </footer> </body> </html>
π§ Template Syntax Overview
Astatine uses @ prefixed directives and {{ }} for echoed variables.
Control Structures
@if (condition):
...
@elseif (condition):
...
@else:
...
@endif
@foreach ($array as $key => $value):
...
@foreach ($array as $value):
...
@endforeach
@for ($i = 0; $i < 10; $i++):
...
@endfor
@while (condition):
...
@endwhile
Template Inheritance
-
@extends('layout.name'): Specifies the layout the template inherits from. -
@section('name'): ... @endsection: Defines a section block. -
@yield('name'): In a layout, displays the content of a section.
Including Partials
@include('path.to.partial'): Includes another template. Partials always have access to parent data.
Comments
{{-- This is a comment --}}: Comments are not rendered in the final HTML.
Echoing Data
-
{{ $variable }}: Echoes escaped data (prevents XSS). -
{!! $variable !!}: Echoes unescaped data (use carefully!).
π€ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Fork the repository.
-
Create your feature branch (
git checkout -b feature/amazing-feature). -
Commit your changes (
git commit -m 'Add some amazing feature'). -
Push to the branch (
git push origin feature/amazing-feature). -
Open a Pull Request.
π License
The MIT License (MIT). Please see License File for more information.