New in Statie 4.5: Twig Support

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" %}
        {% include "_snippets/menu.twig" %}

            {% block content %}{% endblock %}

        {% include "_snippets/footer.twig" %}

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
    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:

{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.latte"

-{include "postMetadata"}
+{include "_snippets/postMetadata.latte"}

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

-            layout: 'post'
+            layout: '_layouts/post.latte'

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.


  Continue Learning

What do you think?

GitHub RSS @votrubaT Runs on Statie Hosted on GitHub

Like what I write about? Hire me & we can work together