Skip to content

B_Template: Deep-sixing Includes

August 10, 2009

B_Template is the first installment in my B-Suite of custom types, and can be used to apply multiple consecutive changes by regular-expression to a text-file. The text-file is then sent to the client as the final content body. I built this to separate the front-end of my web applications, from my middleware code, and avoid an octopus-like tangle of included files. The current B-Suite source can be checked out from the Google Code.

Typically you’ll use the Include tag to dynamically patch together each webpage from a header, footer, sidebar, code libraries, etc. I’ve found this becomes difficult to manage as the size of the website, or number of participants, increase. Instead, I wanted to keep the front-end self contained, and then dynamically apply generated content.

For a very simple example, an HTML page can stand alone, and be easily managed with many available tools.

<html lang="en">
<meta charset="UTF-8">
<title>Insert Title Here</title>
<h1>Insert Header Here</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

A script file can then pull together the data, and apply it to the above template.


Inline( -Database='db',
-SQL='SELECT `title`, `header` FROM `tb` LIMIT 1');

'Insert Title Here'=Field('title'),
'Insert Header Here'=Field('header'));



Of course, the above is an exceedingly simple example. You can also use regular expressions to specify changes, and structure your usage more programatically using member tags.


Local('mytemplate' = B_Template);
#mytemplate -> Location('/path/to/template.html');
#mytemplate -> Apply(RegExp(-Find='(?<=<title>).*(?=</title>)', -Replace='My Title'));
#mytemplate -> ServeAtEnd;


There’s control over when the current instance of the template is served to the client, what MIME type is used, and whether to apply the changes to the current instance or start again with the original template. Using B_Template, you can also extract sub-templates from the original, which allows example structures in the original template to be looped over and appended to with changes.

There’s more information in the project wiki — or will be shortly. I’d love any feedback, so please leave a comment with your thoughts.

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: