PHP code example of taujor / phpssg

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

    

taujor / phpssg example snippets


// 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]);
    }
}

// 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)();
    }
}

<!-- src/views/layouts/base.php -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?= $content 

// 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)
        );
    }
}

// src/utilities/Locate.php
 namespace Taujor\PHPSSG\Utilities;

use Phar;
use Composer\Factory;

class Locate {
    private static ?string $root = null;

    public static function root(): string {
        if(self::$root === null) Phar::running() ? self::$root = getcwd() : self::$root = dirname(Factory::getComposerFile());
        return self::$root;
    }
}

class Hero extends Composable {
    protected function _beforeInvoke(): void {
        $this->timestamp = time();
    }

    public function __invoke(): string {
        return "<p>Generated at {$this->timestamp}</p>";
    }
}

class Title extends Renderable {
    protected function _beforeExtract(array &$data, string &$path): void {
        $data['text'] = htmlspecialchars($data['text']);
    }

    protected function _afterRender(array &$data, string &$html): void {
        $html = trim($html) . "\n";
    }

    public function __invoke(string $text): string {
        return $this->render('components/title', ['text' => $text]);
    }
}

class Post extends Buildable {
    protected function _beforeRender(array|object &$data): void {
        $data->slug = strtolower(str_replace(' ', '-', $data->title));
    }

    protected function _afterRender(array|object &$data, string &$html): void {
        $html .= "\n<!-- Built on " . date('c') . " -->";
    }

    protected function _afterWrite(string &$file, int|false &$bytes): void {
        echo "✅ Wrote {$bytes} bytes to $file\n";
    }

    public function __invoke(array|object $data): string {
        return "<h1>{$data->title}</h1><p>{$data->content}</p>";
    }
}




esenters\Pages\Post;

$data = [
    (object) [
        'id' => 1,
        'slug' => 'first-post',
        'title' => 'First Post',
        'content' => 'Hello'
    ],
    (object) [
        'id' => 2,
        'slug' => 'second-post',
        'title' => 'Second Post',
        'content' => 'World'
    ],
];

// {{}} are the default delimiters for placeholders you can set any delimiters you like separated by whitespace
// creates two files in the build directory "posts/post-1.html" and "posts/post-2.html"
Post::build("/posts/post-{{id}}.html", $data, "{{ }}"); 

// compile also supports placeholders
Post::compile("/posts/post-3.html", (object)[
    'id' => 3,
    'slug' => 'third-post',
    'title' => 'Third Post',
    'content' => 'Hello Again'
]);





esenters\Pages\Post;

$rootDir = Locate::root(__DIR__);
$viewsDir = Locate::views("/src/custom_views"); // relative to root - returns __DIR__ . "/src/custom_views"
$cacheDir = Locate::cache("/custom_cache"); // relative to root - returns "/src/custom_views/custom_cache"
$hashDir = Locate::hashes("/custom_hashes"); // relative to cache - returns "/src/custom_views/custom_cache/custom_hashes"
$proxyDir = Locate::proxies("/custom_proxies"); // relative to cache - returns "/src/custom_views/custom_cache/custom_proxies"
$buildDir = Locate::build("/custom_build"); // relative to root - returns __DIR__ . "/src/custom_views"
$templateExt = Locate::engine(".twig"); // configures the file extenstion expected by the render method of Renderable

Post::compile("/posts/post-1.html", (object)[
    "id" => 1,
    "slug" => "first-post",
    "title" => "First Post",
    "content" => "Hello World"
]); 

bash
git clone https://github.com/Taujor/php-static-site-generator-skeleton
text
src/
└── presenters/
    ├── components/
    │   └── Title.php
    ├── layouts/
    │   └── Base.php
    ├── pages/
    │   └── Home.php
    views/
    ├── components/
    │   └── title.php
    └── layouts/
        └── base.php