Adding Social Media Sharing Buttons to a Nikola Site
In November 2011 I migrated my site from Joomla to Nikola.
Unlike Joomla (and, for that matter, Wordpress/Drupal), Nikola is a static site generator (SSG): when adding content to the site I invoke Nikola and it generates a static files for each of the pages in the site (one of which you're reading now).
This provides significant security and performance benefit. There's no PHP in the background calculating what to serve to you, the file is simply loaded from disk and served on request, reducing both response time and attack surface.
However, because responses are not dynamically calculated, there are things that become harder as a page cannot dynamically include something based upon the user's request.
Social Media sharing icons are an example of this.
In a dynamic site, we might update a template to include
<div class="social-icon">
<?php
// Broken up for readability on smaller screens
$t = urlencode($page->title);
$p = urlencode($_SERVER['REQUEST_URI']);
$d = "https://www.bentasker.co.uk";
?>
<a href="https://twitter.com/share?text=<?php echo $t; ?>&url=<?php echo $d . $p; ?>&via=bentasker">
Tweet This
</a>
</div>
This injects the title of the current page, as well as the path of the page being requested (in practice you'd use one of the CMS's methods rather than the $_SERVER
superglobal, but it allows the example to fit into a small screen more easily).
In a SSG we need to use a slightly different approach. Although the underlying principle is similar, rather than acquiring details from the request, we instead need to ask Nikola's templating engine to include specific details from the post so that we can display preconfigured buttons like these:
This post details the process of adding social media sharing icons to Nikola post pages - it's more or less exactly the process I used to add icons to my own site.