Simplified Site Management: The Power of SSI (Part 3 of 3)

Posted on Wed 30 March 2022 in Programming

Welcome back to the final installment of our exploration into Simplified Site Management using Server Side Includes (SSI). If you haven't followed the previous episodes, I encourage you to catch up to fully grasp the concepts we're about to dive into. In our previous discussions, I promised to provide you with a visual representation in the form of a crude flowchart, and I'm here to deliver on that promise. Be sure to click on it to get the full view, and feel free to keep it open in another window as a reference.

 

As you've been following along, you might be wondering where all of this is leading. What's the grand vision behind splitting our web pages into various files and using SSI to orchestrate them seamlessly? Well, let's delve into that now.

 

The core idea here is to enable the editing of specific sections of a website within individual files while safeguarding against unintentional and site-wide changes. It's all about maintaining well-formed markup across the board. Well-formed markup is the bedrock of a stable and functional website. It ensures that every tag opened is properly closed, except for singular instance tags like "img," which do not require a closing counterpart.

 

Imagine you're responsible for maintaining a website with a consistent left-hand navigation menu across its many pages. Now, with traditional methods, altering this menu could be a daunting task, requiring updates across numerous files. This complexity increases the likelihood of errors and site-wide disruptions.

 

However, by employing the strategy we've been discussing, you can make changes to that menu in a single file, such as "left_menu.html," knowing that your modifications will exclusively affect that specific part of the website. It's like precision surgery for your site updates—no accidental changes to the header or content sections.

 

Let's take …

Continue reading

SSI for CMS (Part 1 of 3): Enhancing Web Development Efficiency

Posted on Mon 28 March 2022 in Programming

Content Management Systems (CMS) are a fantastic tool for individuals who are not well-versed in HTML, but what about those of us who are familiar with HTML, CSS, and other web development technologies? In this three-part series, I'll introduce you to an alternative approach that combines the best of both worlds, allowing developers like us to leverage our skills while benefiting from the convenience of a CMS.

 

CMSs are an excellent solution for setting up websites for clients, offering convenience and often cost savings by eliminating the need for frequent professional updates. If you invest the time to learn even some of their basic features, you can effectively serve your web design clients while keeping their expenses in check.

 

However, many years ago, while working with Apache on a web project, I found myself in a situation where I needed a rapid method for generating content for each page while maintaining consistent headers, footers, menus, and other shared elements. At the time, I may have been unaware of CMS options, or perhaps they were not as readily available as they are today. Regardless, I developed a unique approach to page creation that allowed me to update shared site components without having to modify multiple files.

 

Later on, I delved into ASP programming and Microsoft IIS, but more recently, I've returned to Apache for two web projects. While PHP was an option for including headers and footers in each page, I found it led to an excessive amount of redundant code in each file.

 

To streamline the process, I turned to Server Side Includes (SSI), a technology built into Apache that enables developers (or anyone else) to assemble a web page from one or more files. This method allows for the creation of dynamic pages with reusable components. Let's take a …

Continue reading

Effective Redirects: A Quick Guide

Posted on Fri 18 March 2022 in Programming • Tagged with php, perl

I'll admit, this might serve as a handy reminder for me more than anything else, but let's dive in anyway.

 

Recently, I delved into the world of PHP 5.x and its header() function. During my exploration, I stumbled upon an interesting piece of information:

 

"HTTP/1.1 requires an absolute URI as an argument to the 'Location:' header, including the scheme, hostname, and absolute path, but some clients accept relative URIs."

 

Now, I don't know about you, but RFCs (Request for Comments, essentially internet standards documents) can be incredibly dull. So, I'm grateful to the sleep-deprived souls who unearthed this gem for the rest of us.

 

Following this standard doesn't just matter for PHP; it can also solve issues in other languages, like Perl. In Perl, many folks wisely use CGI.pm for handling redirects, and the CGI manual dishes out the same advice: utilize full URLs for redirects.

 

Here's a mini guide to redirection (basically, you just print out the redirect):

 

```perl

#!c:\Perl\bin\perl.exe

 

use strict;

use warnings;

 

use CGI;

my $query = CGI->new();

 

# This is the wrong way to redirect

# because the URL is missing the domain

# information:

print $query->redirect( "/site/admin/index.shtml" );

 

__END__

```

 

Notice that with the wrong approach, the address bar displays the program that initiated the redirect. If the user refreshes their browser, this program will be called again, which isn't ideal. Users see a dialog box (in IE) upon refreshing with this type of redirect, leading to a subpar user experience and potentially unforeseen consequences for the underlying database.

 

Now, here's the right way to redirect:

 

```perl

#!c:\Perl\bin\perl.exe

 

use strict;

use warnings;

 

use CGI;

my $query = CGI->new();

 

# This is the right way to redirect

# because we use a full URL

print $query->redirect …

Continue reading

Dynamically Adding Fields with jQuery

Posted on Thu 27 January 2022 in Programming • Tagged with javascript, jQuery

The jQuery JavaScript library provides elegant solutions to various programming challenges. While browsing a jQuery email list, I came across a simple problem that even a novice JavaScript coder like me could tackle. I'll simplify it here for the sake of clarity. Essentially, the original poster wanted to add form fields to a table while incrementing a field counter in both the HTML and the ID and name tags. To add a bit more complexity, the field numbers needed to be padded with leading zeros. Adding table rows with jQuery is straightforward, and it turns out that adding leading zeros is quite simple too. I appreciate JavaScript's "everything is an object" approach, which even extends to built-in objects like the String object. Let's start with that.

 

The String object lacks a built-in method for repeating characters. After a quick internet search, I found an efficient solution. To pad leading zeros, we first need a method to repeat a character. Although this explanation may skip the details of how the prototype works, you might want to research it further.

```javascript

// Add a repeat method to the String object

String.prototype.repeat = function(n)

    return new Array(n + 1).join(this);

;

```

Now we can use `'0'.repeat(3)` to generate a string of three zeros ("000").

 

Next, let's create a short padding routine. When writing code, it's wise to think ahead and consider whether you'll need similar functionality in the future. Can you make it dynamic? Here's the padding function I came up with:

```javascript

// Add a leading_zeros function

var leading_zeros = function(n, total_digits)

    n = n.toString();

    return '0'.repeat(total_digits - n.length) + n;

;

```

To make this work with jQuery, I prefer to create a jQuery plugin. This allows us to add a method to the jQuery object, making it easy to …

Continue reading