Staticka generates HTML pages from Markdown using PHP.
Inspired by popular static site generators like Hugo and Jekyll, Staticka is an extensible PHP-based static site generator. It mainly converts Markdown content and PHP template files into static HTML.
Install Staticka
via Composer:
$ composer require staticka/staticka
NOTE: The usage below only consists on how to use Staticka as a library and not as a commonly used static site generator. For the applications that this library is being used, kindly check Console or Expresso.
Create a new file named index.md
:
index.md
# Hello World!
This is a sample Markdown file.
Then add it as a page to Staticka
instance:
index.php
use Staticka\Factories\PageFactory;
$factory = new PageFactory;
$site = new Staticka\Website;
$file = __DIR__ . '/index.md';
$site->add($factory->file($file));
$site->build(__DIR__ . '/public');
To compile the pages, run the php index.php
command:
$ php index.php
To see the output, open public/index.html
in a web browser.
public/index.html
<h1>Hello World!</h1>
<p>This is a sample Markdown file.</p>
Staticka supports Front Matter in which can add predefined variables in a specific content. Special variables like path
, link
, and title
are also generated by default but can be overwritten inside the content.
index.md
---
link: brave-world
---
# Hello World!
This is a sample Markdown file.
build/brave-world/index.html (after running php index.php
in the terminal)
<h1>Hello World!</h1>
<p>This is a sample Markdown file.</p>
The page properties that will be used by Staticka can be found on Staticka\Contracts\PageContract
:
// Content of the page
PageContract::DATA_BODY = 'body';
// Specific name of the page
PageContract::DATA_NAME = 'name';
// The URL path of the page
PageContract::DATA_LINK = 'link';
// File path of the template
PageContract::DATA_PLATE = 'plate';
// Title heading for the page
PageContract::DATA_TITLE = 'title';
Staticka can use filters to modify the contents of the specified page. Creating filters require an instance to be implemented in FilterContract
.
index.php
use Staticka\Factories\PageFactory;
use Staticka\Filter\HtmlMinifier;
// NOTE: Add filters in the Layout instance
$layout = new Staticka\Layout;
$layout->filter(new HtmlMinifier);
$factory = new PageFactory($layout);
$site = new Staticka\Website;
$file = __DIR__ . '/index.md';
$site->add($factory->file($file));
$site->build(__DIR__ . '/public');
build/brave-world/index.html (after running php index.php
in the terminal)
<h1>Hello World!</h1><p>This is a sample Markdown file.</p>
Helpers can be used in putting methods inside template files. It must also be implemented in HelperContract
when creating a new helper.
index.md
---
link: brave-world
layout: layout
---
# Hello World!
This is a sample Markdown file.
layout.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $title; ?></title>
</head>
<body>
<p><?php echo $url->set('hello'); ?></p>
<?php echo $content; ?>
</body>
</html>
index.php
use Staticka\Factories\PageFactory;
use Staticka\Helpers\LinkHelper;
// NOTE: Add helpers in the Layout instance
$layout = new Staticka\Layout;
$helper = new LinkHelper('https://staticka.github.io');
$layout->helper($helper);
$factory = new PageFactory($layout);
$site = new Staticka\Website;
$file = __DIR__ . '/index.md';
$site->add($factory->file($file));
$site->build(__DIR__ . '/public');
build/brave-world/index.html (after running php index.php
in the terminal)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<p>https://staticka.github.io/hello</p>
<h1>Hello World!</h1>
<p>This is a sample Markdown file.</p></body>
</html>