Skip to content

Instantly share code, notes, and snippets.

@Awilum
Last active March 13, 2022 17:51
Show Gist options
  • Save Awilum/c6d98572b48494f4d153280748db5483 to your computer and use it in GitHub Desktop.
Save Awilum/c6d98572b48494f4d153280748db5483 to your computer and use it in GitHub Desktop.
Flextype Static Site Generator
<?php
// Add this route to your Flextype Website to generate static website.
flextype()->get('/generate-static-site', function () {
// Set static site directory path
$staticSitePath = '../awilum.github.io/';
// Set static site base url
//flextype('registry')->set('flextype.settings.url', 'CUSTOM_BASE_URL');
// Get entries list
$entriesFiles = filesystem()->find()->files()->in(PATH['project'] . '/entries/');
// Create static site directory
filesystem()->directory($staticSitePath)->create(0755, true);
// Create static site entries
$i = 0;
foreach($entriesFiles as $file) {
$i++;
$entryID = strings($file->getPath() . '/' . $file->getFilename())->replace('/entry.md', '')->replace(ROOT_DIR . '/project/entries/', '')->toString();
$entry = flextype('entries')->fetch($entryID);
echo $i . ' - ' . $entryID . "<br>";
filesystem()->directory($staticSitePath . $entryID)->create(0755, true);
filesystem()
->file($staticSitePath . $entryID . '/index.html')
->put(flextype('twig')->fetch('themes/' . flextype('registry')->get('plugins.site.settings.theme') . '/' . (empty($entry['template']) ? 'templates/default' : 'templates/' . $entry['template']) . '.html', ['entry' => $entry]));
}
// Create static site project index.html for main main entry
filesystem()->file($staticSitePath . '/index.html')->put(file_get_contents($staticSitePath . '/' . flextype('registry')->get('plugins.site.settings.entries.main') . '/index.html'));
filesystem()->directory($staticSitePath . '/' . flextype('registry')->get('plugins.site.settings.entries.main'))->delete();
// Create empty index.html file in static site project directory
filesystem()->file($staticSitePath . '/project/index.html')->put('');
// Create assets directory and copy all assets into the static site directory
filesystem()->directory($staticSitePath . 'project/themes/' . flextype('registry')->get('plugins.site.settings.theme') . '/assets/')->create(0755, true);
filesystem()->directory(PATH['project'] . '/themes/' . flextype('registry')->get('plugins.site.settings.theme') . '/assets/')->copy($staticSitePath . 'project/themes/' . flextype('registry')->get('plugins.site.settings.theme') . '/assets/');
die('Done :)');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment