← Back to context

Comment by em-bee

4 hours ago

my most recent website is fully static html and css. it contains several pages and a menu that is duplicated in all pages.

that menu is maintained manually, and every time a page is added, all copies of the menu have to be updated. at some point that will get tedious. then i have a few options:

i generate the menu using a backend framework. the downside: the website is no longer fully static. i now depend on a backend framework for hosting. static hosting is out. i also have to edit the content through the backend, and i have to continuously maintain the backend for the lifetime of the site.

i generate the menu using a static site builder. the downside: i can no longer edit the content directly in html. i have to keep a source version for the static site generator, and more critically, i have to keep a copy of the site generator in my repo because i want to be able to make changes to this site 10 years from now and not find that the version of the site generator i was using is no longer available for download and my source is not compatible with the new version of the site generator.

i generate the menu using xslt. works, but xslt is no better than javascript security wise. it's possibly even worse. and xslt support may be removed from browsers in the future.

the final option is javascript. using some framework that doesn't need build tools, or something homegrown. i am afraid javascript is the only option that is futureproof while letting me avoid manual work to maintain the menu.

While purists will disagree, a little bit of javascript is fine. Having said that, you are talking about a personal website. Most companies will have a backend of some sort anyway.

Also, here is a little secret. If you want something that is future proof, try something that has been around for decades and still runs large parts of the internet. People will scoff at this, but PHP is actually really neat for personal websites and has been for decades.

A while ago I also found myself looking at static site generators, workflows from github to my host, etc. Eventually I realized I don't update things nearly enough, don't blog, etc.

For similar reasons as you I didn't want to go completely static as that is just too much hassle when doing multiple pages. So I decided to utilize that good old lamp stack to basically do something like this.

    <?php
    $pages = json_decode(file_get_contents('pages.json'), true);
    $page = $_GET['page'] ?? 'home';
    if (!isset($pages[$page])) {
        $page = 'home';
    }
    $pageData = $pages[$page];
    $contentFile = basename($pageData['file']);
    $contentFilePath = "content/{$contentFile}";
    ?>
    <html>
    <head>
        <title><?= htmlspecialchars($pageData['title']) ?></title>  
    </head>
    <body>
        <main>
        <?php include $contentFilePath; ?>
        </main>
    </body>
    </html>

Slightly more, but the base principle is the same. Just 10 lines of php code, and now I can just add pages by uploading their contents and adding them to the json file for whitelisting. For your use case it would be trivial to add a menu based on the json file and I'd be confident that it will still work in 10 years with minimal adjustments.

As a bonus I also rediscovered PHP in itself works really well as a templating language (as you can see) so no need for extra stuff like handlebars. As an extra extra bonus, I can just go to any shared webhosting party and get it running with no issue at all.

I am not saying you'd need to go down this road. But I just want to illustrate how stupidly simple a website can be with "old" basic technologies even if you want some form of backend.