mod_include

What does this mod_include offer ?

Post-processing of variables contained within a variable.

Doesn’t sound too helpful ? Well, maybe not, but I found that during construction of this site that I was frequently replicating 5 lines of text with only a single word altered (the left menu). This was both inefficient and awkward to debug, and didn’t lend itself to keeping a common look and feel for the site as a whole. This extension allows large blocks of text to be stored as a variable, and for the small changable part to be altered just before the result is displayed.

What’s wrong with set anyway ?

When a variable is used in the value entry of a set command the system searches for the current value of the variable and substitutes it immediately. This fixes the value of the variable created so that it never changes, which is good for some applications, but lousy for dynamic content.

Usage

This module is an extension of the normal Apache behaviour, and should be read as an addendum to the basic mod_include commands.

define
This command sets up a variable in the same way as the set directive, except that any variable names used are not parsed at this time, but stored as names until displayed with the macro directive.

var
The name of the variable (macro) to define.
value
The value of the variable (macro).
macro
This command will expand a previously defined variable and replace any instances of the variable named in var in the variable def with the value given in value. Note that like other mod_include commands, the order of the variables is important, and should be declared in the sequence shown below:

var
The name of the variable to replace inside the macro.
value
The value to replace the variable with.
def
The previously defined macro to seach through.

Examples

Change the font for a single phrase:
<!--#define var="FONT_FACE" value="<FONT FACE="Times">$TEXT</FONT>" -->
<!--#macro var="TEXT" value="Hello" def="FONT_FACE" -->
Output:

<FONT FACE="Times">Hello</FONT>
Add a hyperlink:
<!--#define var="LINK" value="<A HREF="$URI">$TEXT</A>" -->
<!--#set var="TEXT" value="Home" -->
<!--#macro var="URI" value="http://minimal.cx/" def="LINK" -->
Output:

<A HREF="http://minimal.cx">Home</A>

Impact

I do have more than just mod_include compiled into Apache, but the difference between the basic version and mine is 5136 bytes in the httpd executable. I don’t think that the time for standard mod_include calls would be adversely effected, as there are only two extra tests for the new words, and performance of those words is not comparable to the old system.

Licen[c|s]e

I’m not interested in making things any more difficult than they already are in this area, so this patch is distributed under the Apache Licence.

Download

Choose from a diff for Apache 1.3.24/1.3.26, or simply grab the entire mod_include.c file. For the merely curious, there is also an online version.

Improvements

The element order checking is not robust for #macro.

Feedback / Bugs / Feature Requests

Just send it to me and I’ll see what I can do to help out. Bear in mind that I’m doing this primarily for myself, so porting to various other versions of Apache is only likely to happen when I need it, but I’m more than happy to help out if required.

Comments are Disabled