Recent Content on the homepage

5-JAN-21

Feature alert: New font-awesome icons on the top nav to match the new "Recent Content" section of the homepage.

With the addition of photos and scribbles it made sense to update the "Recent Entries" on the homepage, which only included writing, to "Recent Content" -- a mashup of photos, scribbles and writing. Here's how I did it.

First, in the database, a stored procedure to join the two tables with UNION. You'll notice I also baked the font-awesome icon names into this dataset; this will make things simpler in the end.

CREATE PROCEDURE `up_GetRecentContent`()
BEGIN
SELECT 
	Title, 
        TimeAdded, 
        (CASE WHEN Category = 1 THEN "photos" ELSE "scribbles" END) AS Category, 
	(CASE WHEN Category = 1 THEN "fas fa-camera-retro" ELSE "fas fa-pencil-alt" END) as Icon,
	`Key` 
FROM tblPhotos_and_Scribbles
UNION
SELECT 
	Title, 
        TimeAdded, 
        "writing" AS Category, 
        "fas fa-pen-alt" as Icon, 
        PermaLink AS `Key` 
FROM vw_GetLog2Entries
ORDER BY TimeAdded DESC LIMIT 10;
END

Here's the current output of the above procedure:

Recent Content on the homepage, 1609851918, writing, fas fa-pen-alt, 2021/01/recent-content-on-the-homepage.html
Stowa, meet Alox, 1609804144, photos, fas fa-camera-retro, 8
Birthday sushi, 1609710404, photos, fas fa-camera-retro, 7
Stained glass window, 1609648239, photos, fas fa-camera-retro, 6
The old cherry tree, 1609539674, photos, fas fa-camera-retro, 5
Last day of the year, 1609468627, photos, fas fa-camera-retro, 4
New features: photos and scribbles, 1609390313, writing, fas fa-pen-alt, 2020/12/photos-and-scribbles.html
Neat & tidy comics, 1609387095, photos, fas fa-camera-retro, 3
Developing our wings, 1609380580, scribbles, fas fa-pencil-alt, 2
Christmas portrait 2020, 1608818400, photos, fas fa-camera-retro, 1

Then, in the Page class, a simple method to call the stored procedure:

public function get_recent_content()
{
	$rc = $this->sql->RunDBProcedure("up_GetRecentContent");
	return $rc;
}

Next, call the method from the index page and pass it to the templating engine:

// recent content
$rc = $page->get_recent_content();
$smarty->assign("recent_array",$rc);

And finally, a section loop in the Smarty template to output the links to the new content, including the inline icon handling:

<h4>Recent Content</h4>
{if $recent_array}
	<p>
	{section name=sec1 loop=$recent_array}
	<a href="/{$recent_array[sec1]['Category']}/{$recent_array[sec1]['Key']}{if {$recent_array[sec1]['Category']} != 'writing'}/{/if}"><i class="{$recent_array[sec1]['Icon']}"></i> {$recent_array[sec1]['Title']}</a>
	{/section}
	</p>
{else}
<p>No recent content.</p>
{/if}

Nice and simple, which is how I like it at my age. (Hey, I can say that now that I'm 40! 😄)