Note: This article was originally written for Textpattern version 4.2. The links below are for different versions:
When I started work on redesigning this site, I also made the switch from WordPress to Textpattern. There was one thing that I really started to miss while developing the site — files for templates. WordPress had made this so easy in its theming system, which uses PHP files, thus allowing the use of an external text editor. Instead, Textpattern forces you to use a textarea in the admin interface, and saves the markup in a database. Since I like to have syntax highlighting and the ability to quickly edit multiple templates at once, I decided to create a little workaround to allow me to use files for both page and form templates.
Warning & Disclaimer
Before I begin, if you are allergic to looking at source code I recommend you look away now. Still here? Then let's begin, and don't say I didn't warn you.
The modifications made here are to the Textpattern 4.2.0 core library files, as such any updates to Textpattern will require you to repeat this process. While unlikely (I run this on all my Textpattern installations), I take no responsibility if your install breaks as a result of making these changes. If you would like a quick and easy method of having static page files only (ie: not forms), then please read this post by Mr. Hicks which explains how to achieve this with a lot less effort.
This guide is based on creating the template folder outside of your textpattern folder at the same level as it. If you would like your templates somewhere else, modify the location in each case below. You will also need to enable "Allow PHP in pages?" in Advanced Preferences of the Admin tab, if you haven't already.
First, the templates for pages. In the textpattern folder, open the file
publish.php. On line 482 is the start of the
textpattern() function (do a search for 'function textpattern' in your favourite text editor). We want to edit this function to try and import a file, should it exist, before looking in the databse for the page HTML.
Replace lines 496 to 498 with the code shown in the Gist below — start copying from
$html. The ... indicates that there is more code not shown, and the
function textpattern() is to show what function you are editing in the file.
Textpattern will now look for page templates in
templates/pages/ folder. Make sure to set up your sections and corresponding page templates as you want them. Create a new file in the pages folder with the same name as the page template you wish to use: default.php, for example, if you wish to overwrite the default page.
Following the same process as for pages above, open the file
textpattern/lib/txplib_misc.php. On line 1589 you will find
function fetch_form($name) (do a search for 'function fetch_form' in your favourite text editor). Replace this function with the one below:
Just one more step left, 404/error pages (error_default in a standard Textpattern install). If you'd like a static error page too, there's one more step you need to do.
In the file
textpattern/lib/txplib_misc.php, on line 1827, you'll find a function called txp_die (do a search for 'function txp_die' in your favourite text editor). Simply replace the code in that file with the code below.
And you're done! Congratulations, you now have a Textpattern install that can use template files!