PHPackages                             staticka/staticka - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. staticka/staticka

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

staticka/staticka
=================

Yet another PHP static site generator.

v0.4.0(1y ago)33.0k13MITPHPPHP &gt;=5.3.0CI failing

Since Apr 4Pushed 1y ago1 watchersCompare

[ Source](https://github.com/staticka/staticka)[ Packagist](https://packagist.org/packages/staticka/staticka)[ Docs](https://roug.in/staticka)[ RSS](/packages/staticka-staticka/feed)WikiDiscussions master Synced 2d ago

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

Staticka
========

[](#staticka)

[![Latest Version on Packagist](https://camo.githubusercontent.com/88df25523b3c63eae82e3073f56dbf760e25303cfc5b89e9a91bb3853ad6db14/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7374617469636b612f7374617469636b612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/staticka/staticka)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/staticka/staticka/blob/master/LICENSE.md)[![Build Status](https://camo.githubusercontent.com/9eb9f75c4548597a4124b9b69c5e41bd4e85d60846d4c582e2ffc1da8dffaa6e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7374617469636b612f7374617469636b612f6275696c642e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/staticka/staticka/actions)[![Coverage Status](https://camo.githubusercontent.com/dfd8e658fff913b92bb73f5ccdc5369f3e36c3e2464639b2f856b5c2a59b543d/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f7374617469636b612f7374617469636b613f7374796c653d666c61742d737175617265)](https://app.codecov.io/gh/staticka/staticka)[![Total Downloads](https://camo.githubusercontent.com/15b9d3a4bb498ed3a584351e8a6b81d17140dd90d7d227042bdddb450820e1ed/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7374617469636b612f7374617469636b612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/staticka/staticka)

A yet-another PHP-based static site generator. This converts [Markdown](https://en.wikipedia.org/wiki/Markdown) content and PHP files into static HTML. It is inspired by popular static site generators like [Hugo](https://gohugo.io) and [Jekyll](https://jekyllrb.com).

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

[](#installation)

Installing `Staticka` is possible via [Composer](https://getcomposer.org/):

```
$ composer require staticka/staticka
```

Basic Usage
-----------

[](#basic-usage)

The following guides below showcase the functionalities of `Staticka`. If a specified requirement wants to create a static blog site using a terminal or wants to create pages with a web-based user interface, kindly check [Console](https://github.com/staticka/console) or [Expresso](https://github.com/staticka/expresso) respectively.

### Simple HTML from string

[](#simple-html-from-string)

`Staticka` can convert simple Markdown content from string into HTML:

```
// index.php

use Staticka\Page;
use Staticka\Parser;

// Creates a new page with the specified body -----
$page = (new Page)->setName('Hello world!');

$page->setBody("# {NAME}\nThis is a sample page.");
// ------------------------------------------------

// Converts the page into an HTML ---
echo (new Parser)->parsePage($page);
// ----------------------------------
```

```
$ php index.php

Hello world!
This is a sample template.
```

In the example above, the `{NAME}` is a placeholder to insert the `name` value from the `Page` class to the body.

### Using `.md` files

[](#using-md-files)

`Staticka` also supports converting the Markdown-based files (`.md`files) by adding the path of the `.md` file to the `Page` class:

```

# Hello World!

This is a sample **Markdown** file!
```

```
// index.php

// ...

// Specify the path of the Markdown file -----
$file = __DIR__ . '/app/pages/hello-world.md';

$page = new Page($file);
// -------------------------------------------

// ...
```

```
$ php index.php

Hello World!

This is a sample Markdown file!
```

### Adding Front Matter, additional data

[](#adding-front-matter-additional-data)

`Staticka` supports [Front Matter](https://jekyllrb.com/docs/frontmatter) in which can add predefined variables in a specific content.

```

---
link: hello-world
---

# Hello World!

The link is **{LINK}**.
```

```
$ php index.php

Hello World!
The link is hello-world.
```

To use the variable inside a content, use the curly brackets (`{}`) format (e.g., `{NAME}`).

### Building HTML pages

[](#building-html-pages)

Multiple `Page` classes can be converted into `.html` files with their respective directory names using the `Site` class:

```
// index.php

// ...

use Staticka\Site;

// ...

// Builds the site with its pages ------------
$site = new Site($parser);

$file = __DIR__ . '/app/pages/hello-world.md';
$site->addPage(new Page($file));

$site->build(__DIR__ . '/app/web');
// -------------------------------------------
```

```
$ php index.php
```

```

Hello World!
The link is hello-world.
```

The `Site` class can also empty a specified directory or copy a directory with its files. This is usable if the output directory needs CSS and JS files:

```
app/
├─ web/
│  ├─ index.html
styles/
├─ index.css

```

```
// ...

// Empty the "output" directory ---
$output = __DIR__ . '/app/web';
$site->emptyDir($output);
// --------------------------------

// Copy the "styles" directory to "output" ---
$styles = __DIR__ . '/styles';
$site->copyDir($styles, $output);
// -------------------------------------------

// ...
```

Adding data that can be applied to all pages is also possible in the same `Site` class:

```
// ...

$data = array('ga_code' => '12345678');
$data['website'] = 'https://roug.in';

$site->setData($data);

// ...
```

Note

The data provided in the `Site` class can also be accessed inside a content by using the curly braces format (`{}`).

### Adding template engines

[](#adding-template-engines)

Building HTML pages from Markdown files only returns the content itself. By adding a third-party template engine, it makes it easier to add partials (e.g., layouts) or provide additional styling to each page. To add a template engine, a `Render` class must be used inside the `Parser` class:

```

---
name: Hello world!
link: hello-world
plate: main.php
---

# This is a hello world!

The link is **{LINK}**. And this is to get started...
```

The `plate` property specifies the layout file to be used when parsing the page. In this example, the `main.php` is the layout to be used in the `hello-world.md` file:

```

>

```

Note

The `$html` variable from the example above is a predefined variable for returning the content from the page that is parsed. The additional data from the `Page` class are also predefined as a variable (e.g., `$link` from `link`, `$name` from `name`).

After creating the template file (e.g., `main.php`), specify the `Parser` class to render templates using the `Render` class:

```
// index.php

// ...

use Staticka\Parser;
use Staticka\Render;

// ...

// Path where the "main.php" can be located ---
$path = __DIR__ . '/app/plates';
// --------------------------------------------

// Sets the Render and Parser ---
$render = new Render($path);

$parser = new Parser($render);
// ------------------------------

// Render may be added to Parser after ---
$parser->setRender($render);
// ---------------------------------------

// ...
```

Then running the script will convert the `.md` file to a compiled `.html` with the defined template:

```
$ php index.php
```

```

>

  Hello world!

  This is a hello world!
  The link is hello-world. And this is to get started...

```

To implement a custom template engine to `Staticka`, implement the said engine to the `RenderInterface`:

```
namespace Staticka\Render;

interface RenderInterface
{
    /**
     * Renders a file from a specified template.
     *
     * @param string               $name
     * @param array $data
     *
     * @return string
     * @throws \InvalidArgumentException
     */
    public function render($name, $data = array());
}
```

### Setting layouts

[](#setting-layouts)

A `Layout` class allows a `Page` class to use various filters and helpers. It can also be passed as a `class-string` in the `.md` file:

```
// index.php

use Staticka\Layout;

// ...

$pages = __DIR__ . '/app/pages';

// Define the layout with the name "main.php" ---
$layout = (new Layout)->setName('main.php');
// ----------------------------------------------

// ...

// Set the layout into the page -------------
$page = new Page($pages . '/hello-world.md');

$site->addPage($page->setLayout($layout));
// ------------------------------------------
```

Note

If a name is specified from the `Layout`, there is no need to specify the `plate` property from the `.md` file.

It is also possible to specify a `Layout` class in the `.md` file by specifying it to the `plate` property. Using this approach requires a `RenderInterface` attached to the `Parser` class:

```
namespace App\Layouts;

use Staticka\Layout;

class HomeLayout extends Layout
{
    /**
     * Specifies the plate to be used as the layout.
     *
     * @var string
     */
    protected $name = 'home.php';
}
```

```

---
name: Hello world!
link: hello-world
plate: App\Layouts\HomeLayout
---

# This is a hello world!

The link is **{LINK}**. And this is to get started...
```

If the `Layout` class requires complex dependencies, the `Parser` class must specify a container to easily identify the specified layout instance:

```
namespace App\Layouts;

use App\Complex;
use Staticka\Layout;

class ComplexLayout extends Layout
{
    protected $complex;

    public function (Complex $complex)
    {
        $this->complex = $complex;
    }

    // ...
}
```

```
// index.php

use App\Layouts\ComplexLayout;
use Rougin\Slytherin\Container\Container;
use App\Complex;

// ...

// Define the complex layout... ---------
$layout = new ComplexLayout(new Complex);
// --------------------------------------

// ...then add it to the container... ---
$container = new Container;

$name = ComplexLayout::class;

$container->set($name, $layout);
// --------------------------------------

// ...

// ...and set the container to the parser ---
$parser->setContainer($container);
// ------------------------------------------

// ...
```

Note

If the container is not specified, the `ReflectionContainer` from [Slytherin](http://github.com/rougin/slytherin) is initialized by default.

Extending and customization
---------------------------

[](#extending-and-customization)

With the philosophy of `Staticka` to be an extensible and scalable static site generator, the following guides below are use-cases for `Staticka` turning it into a content management system (CMS) or a fully functional static blog website:

### Modifying with filters

[](#modifying-with-filters)

A `Filter` allows a page to be modified after being parsed:

```
// index.php

use Staticka\Filter\HtmlMinifier;

// ...

// Set the layout class for "main.php" ---
$layout = new Layout;

$layout->setName('main.php');
// ---------------------------------------

// Minifies the HTML after parsing the page ---
$layout->addFilter(new HtmlMinifier);
// --------------------------------------------

// ...
```

```
$ php index.php
```

```

>Hello world!This is a hello world!The link is hello-world. And this is to get started...
```

To create a custom filter, implement it using the `FilterInterface`:

```
namespace Staticka\Filter;

interface FilterInterface
{
    /**
     * Filters the specified code.
     *
     * @param string $code
     *
     * @return string
     */
    public function filter($code);
}
```

Tip

Please see [FILTERS](https://github.com/staticka/staticka/blob/master/FILTERS.md) page for showcasing the list of available filters.

### Custom methods using helpers

[](#custom-methods-using-helpers)

A `Helper` provides additional methods inside template files:

```
// index.php

use Staticka\Helper\LinkHelper;

// ...

// Set the layout class for "main.php" ---
$layout = new Layout;

$layout->setName('main.php');
// ---------------------------------------

// Add a "$url" variable in templates ---
$url = new LinkHelper('https://roug.in');

$layout->addHelper($url);
// --------------------------------------

// ...
```

```

>
