Textpattern tips, tutorials and code snippets

Using files for page templates in Textpattern

One of Textpattern’s drawbacks has always been that page templates are saved in the database. Editing them means using the textarea in the TXP admin interface, but I’m sure most people would prefer to use their favourite text editor.

Here’s a workaround that works in TXP 4.2.0 (I haven’t tried other versions).

  1. Enable ‘Allow PHP in pages?’ in Advanced Preferences if you haven’t already
  2. Set up your sections and corresponding page templates as you want them
  3. Copy and paste everything in your page template into a php file (such as ‘template.php’)
  4. Create a ‘pages’ folder in your textpattern directory and save the file there
  5. Replace the content of your page template in TXP admin with:
<txp:php>echo parse(file_get_contents(txpath.'/pages/template.php'));</txp:php>

From there on in, you can edit the php file. There may be a performance hit doing this, but all TXP tags work. Much easier!

11 Comments Comment feed

I’m using cnk_versioning for this purpose which is a way more elegant solution, imho.

@Marcus – care to write about your method and submit to TXP Tips?

@Jonathan – when I’ll find the time, I’ll get back to you. Till then, keep on the good work with TXP Tips! :-)

I followed their initial conversation on twitter and Sam Brown suggested this one:
<txp:php>include(txpath.’/pages/default.php’);</txp:php>

Jon updated his post, but now I see he has removed that one. Any idea why?

@Filipe: It was Robert’s (wet) recommendation to do so. And a follow up tweet.

  • Joran Aerns
  • 5 August 2010

Am I wrong when my first take on this is: modules could be treated as such?

I so hoped that this would be a plugin-free easy solution, just to find out that it doesn’t work with the Multi-Language-Plugin (MLP).

If you use the MLP to localise text within templates via ##localised_text## tags, those will stop switching languages and are displayed straight as tags with the double hashes…. :-(

I use the cnk_versioning PlugIn to edit my templates with the Eclipse IDE and to use the TXP Tags with the Eclipse Code suggestion. I started to add the TXP Tags in the Eclipse Templates. So when i start typing <txp:… i get a list to choose from. I still need to add a bunch of TXP Tags to the templates but if anyone wants the XML file to import to Eclipse i would gladly share them with the community.

Screenshot from Eclipse

  • Gary
  • 8 December 2010

@superfly: Oh yes, please share. I’m currently using JEdit but having txp tag completion/suggestion is worth the switch.

Well. I am looking for an elegant fail safe way of working with external Pages and Forms.

the warning that comes with cnk_versioning leaves me cold! I have no confidence is using it.

“Please use this with care! Your pages/forms/css will be deleted from the database, when you delete the corresponding files.”

Two questions: what is Eclipse IDE? Where do i get it.

and is the cnk_versioning a safe choice given this warning:
“ Please use this with care! Your pages/forms/css will be deleted from the database, when you delete the corresponding files”

PS i just thought of a solution: one could have a mirrored back-up of the external _templates directory.

Add a comment

Use Textile help to style your comments