PHPackages                             taujor/phpssg - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. taujor/phpssg

ActiveLibrary[Templating &amp; Views](/categories/templating)

taujor/phpssg
=============

A lightweight static site generator, utilizing composable templates in pure PHP. This project uses a presenter -&gt; view invokable component classes pattern to provide clean, composable, vanilla, php developer experience without a third-party templating engine.

1.1.1(6mo ago)59322MITPHPPHP &gt;=8.1

Since Oct 12Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/Taujor/php-static-site-generator)[ Packagist](https://packagist.org/packages/taujor/phpssg)[ GitHub Sponsors](https://github.com/sponsors/taujor)[ RSS](/packages/taujor-phpssg/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (5)Used By (0)

💖 PHPSSG is open source and free.
If you find it useful, please consider [sponsoring me](https://github.com/sponsors/taujor) to support continued development.

PHPSSG (Personal Home Page Static Site Generator)
=================================================

[](#phpssg-personal-home-page-static-site-generator)

A **lightweight, PHP-native static site generator** for building **composable templates**.
PHPSSG uses **invokable component classes**, **output buffering**, and **plain PHP templates** to provide a clean developer experience — no third-party templating engine required, but you are welcome to use one.

---

Features
--------

[](#features)

- **Plain PHP templates** – no special syntax to learn.
- **Invokable components** – use components like `$header()`.
- **Component based routing** – `Buildable` components get methods able to write the html they generate to a file.
- **Hooks** – Hooks are available to easily inject your own custom code to manipulate content during the component lifecycle.
- **Centralized render helper** – avoids repeated `ob_start()` / `ob_get_clean()`.
- **Nesting &amp; composition** – layouts can include multiple components.
- **PHAR Support** – easily ran as a phar if desired.
- **Slots &amp; data passing** – inject content into templates easily.
- **Incremental builds** – files are rewritten only if content changes (xxh3 hash comparison).
- **Caching** – file paths and contents are hashed and cached to disk prioritizing thread safety.
- **Native debugging** – works seamlessly with standard PHP tools.
- **PSR-4 compliant** – fully autoloadable via Composer.
- **Abstract Classes** – `Renderable`, `Composable`, `Buildable` help define component APIs, they are fundamental to PHPSSG.
- **IDE-friendly** – docblocks provide autocomplete, type hints, and method signatures.
- **Highly portable** – works in any PHP 8.1+ environment.
- **Flexible structure** – There are no rules that force you to structure your project a certain way, I have made suggestions and set some reconfigurable defaults but beyond that I leave everything completely up to you.
- **Fast content comparison** – via [xxh3](https://php.watch/versions/8.1/xxHash) hashing.

---

Requirements
------------

[](#requirements)

- PHP 8.1+ (xxh3 hashing)
- Composer (autoloading)
- PHP-DI (dependency injection)

---

Quick start
-----------

[](#quick-start)

Clone the skeleton template repo or visit the repo and click "Use this template"

```
git clone https://github.com/Taujor/php-static-site-generator-skeleton
```

---

Installation
------------

[](#installation)

Install via Composer:

```
composer require taujor/phpssg
```

Run a local development server:

```
php -S localhost:8080 public/index.html
```

---

Usage
-----

[](#usage)

### Layouts

[](#layouts)

Layouts are typically **Renderables**:

```
// src/presenters/layouts/Base.php
use Taujor\PHPSSG\Contracts\Renderable;

class Base extends Renderable
{
    public function __invoke(string $content): string
    {
        // the directory specified here is relative to your views directory ("src/views" by default)
        return $this->render("layouts/base", ["content" => $content]);
    }
}
```

---

### Components

[](#components)

Components are typically **Renderables** (like layouts) or **Composables** :

```
// src/presenters/components/Heading.php
use Taujor\PHPSSG\Contracts\Composable;

class Heading extends Composable {
    function __construct(private Title $title, private Subtitle $subtitle) {}
    function __invoke(): string {
        return ($this->title)() . ($this->subtitle)();
    }
}
```

`Title` and `Subtitle` are **Renderables** (in this example) they have their own respective template files, however our `Heading` class simply takes these two components and "composes" them into a new component (via string concatenation in this case). Hence it does not require its own template file but can still be invoked just the same as any other presenter.

```

DOCTYPE html>

    Document

```

The `render` method of `Renderable` extracts variables into the template and returns the output as a string.

---

### Pages

[](#pages)

Pages are typically **Buildables**, like **Composables** they are self-contained, and do not need separate view templates. They inherit the `compile` and `build` methods which output html files to the build directory (`public` by default):

```
// src/presenters/pages/Post.php
use Taujor\PHPSSG\Contracts\Buildable;

class Post extends Buildable
{
    public function __construct(private Base $base, private Title $title, private Body $body) {}

    public function __invoke(object $data): string
    {
        return ($this->base)(
            ($this->title)($data->title) . ($this->body)($data->content)
        );
    }
}
```

Pages often combine components and layouts. The `$data` argument is passed to the page during the build process. `$data` is then passed to components via their respective `__invoke()` methods. Then finally the layout wraps the combined HTML content.

---

### Utilities

[](#utilities)

Utilities are typically helper classes that provide additional functionality to phpssg. You can use utility classes to implement things like alternative renderers (twig for example), provide custom methods to your components, etc.

```
// src/utilities/Locate.php
