New in Statie 4.5: Twig Support

This post was updated on September 2018
Updated with Statie 5, NEON → YAML and Twig.

Statie supports YAML and Symfony Dependency Injection for some time. But you wanted more! You wanted Twig. Sculpin and all the other PHP Static Website generators have it.

So there you go! Enjoy

See pull-request

3 Steps to Your First Twig Page on Statie

1. Prepare Layout _layouts/default.twig

<!DOCTYPE html>
<html lang="en">
    {% include "_snippets/head.twig" %}
    <body>
        {% include "_snippets/menu.twig" %}

        <div>
            {% block content %}{% endblock %}
        </div>

        {% include "_snippets/footer.twig" %}
    </body>
</html>

2. Create Template contact.twig

---
layout: "_layouts/default.twig"
---

{% block content %}
    <h1>Call me</h1>

    <a href="tel:+420776778332">+420 <strong>776 778 332</strong></a>
{% endblock %}

3. Enable Twig

# statie.yml
parameters:
    templating: twig

And you're ready to go!

How to Upgrade to Statie 4.5?

Update it in composer:

composer require symplify/statie 4.5

And try:

vendor/bin/source generate

It will probably fail because there was one change in templating reference.

Before the paths of included files were just a file name and the full path was guessed. This practice is common in Nette and Symfony Controllers, so I used as a starting point. But it caused many WTFs and name conflicts like:

/_snippets
    /post
        detail.latte
    /lecture
        detail.latte
{include "detail"}

Which file will is used? Or is it a block import? Magic :)

How to do it better with a principle of the least surprise?

Clear, Obvious File Naming

All files in _layouts and _snippets are now referenced by they full relative path to source (usually /source directory):

-layout: "default"
+layout: "_layouts/default.twig"
 ---

-{% include "postMetadata" %}
+{% include "_snippets/postMetadata.twig" %}

Do you use Generators? Don't forget to upgrade them too:

 parameters:
     generators:
         posts:
-            layout: 'post'
+            layout: '_layouts/post.twig'

Already Running on Statie 4.5

Check diffs of these merged pull-requests so you have the idea how small the change really is:

How to Migrate Latte to Twig as Well?

If you use Statie, you're probably running on Latte. In the case you prefer Twig, I guess you're already frustrated from annoying Latte to Twig migration you have ahead of you.

Again, check these diffs, so you have the idea how big that change really is:

I feel you. You can stay with Latte until... just kidding. That's a lot of work, right? Well, I'm not that hardworking, don't worry. I'm a lazy bastard, so I made a package for Latte to Twig migration.

Enjoy!


Typo? Fix it, please  and join 49 people who build this website