Browser based template editor
Thursday, September 4th, 2008 at 2:28pm, in File Management, written by Stefan Ashwell
A common feature to CMS scripts is a browser based means of editing your templates. This can be helpful if you manage your site remotely or on the move quite often. In this tutorial we walk through how to create a simple template editor in PHP.
This tutorial presumes that your system's templates are separated from your main PHP. You might be using a template language where all your templates are .tpl files for example. The directory these templates are kept must be writable (CHMOD 777).
This tutorial is split into two main parts: The form and the processing that occurs when you hit submit.
The form
First off we will need to open the file and get the current contents of the file, we will then populate a textarea with these contents so we can edit them.
$file = 'template.tpl';
$handle = fopen($file, 'r') or die('Unable to open the file');
$templateInfo = fread($handle, filesize($file));
fclose($handle);
In the code above we declare a variable called $file with the path to the file we want to open. We then use the fopen function to open the file. the second parameter of 'r' is passed telling PHP that we only want to 'read' the file.
Next we use the fread function to get the contents of the file and put them in a variable that we've called $templateInfo. The second parameter passed tells fread how much of the file we want to get, in this case we pass the filesize of our template file so that it gets the whole file. Finally, we're done with the template now so we close the file.
Now we have the contents of our template we can create a form and put the contents of the template in a textarea for editing.
<form action="editor.php" method="post">
<textarea name="template"><?=$templateInfo;?></textarea>
<input type="submit" name="from_submit" value="Save" />
</form>
We now have a very basic form with a textarea populated by our templates contents and 'Save' button. Now all we need to do is write the code that will process the form submission.
Form processing
if ( $_POST['form_submit'] ) {
$file = 'template.tpl';
$handle = fopen($file, 'w') or die('Unable to open the file');
fwrite($handle, $_POST['template']);
fclose($handle);
}
Other than checking for a form submission this code is very similar to before with a couple of differences. You'll notice that we open the file again, but this time using 'w' as the second parameter rather than 'r'. This tells PHP that we want to write to the file with completely new contents, so it opens it as writable and gets rid of everything that's in there, leaving an empty file for us to put our newly edited contents into. Next we use the fwrite function to put everything that was entered into the textarea in the file. Close the file and we're done - template edited!
Getting more advanced
You may want to get a little more advanced with this tutorial and have a fully featured code editor to help you edit your template. I would recommend checking markItUp! out for this.
Share and Enjoy:
Subscribe to Total PHP: RSS | Email
Related Posts:
- Creating a text or csv file of information from your database
Recommended resources
Recent articles
- How to Read an RSS Feed with PHP 5
- Performing searches on strings using strpos
- PHP form validation basics
- Using a global configuration file
- Creating a text or csv file of information from your database
- Using an autoload function to make your life easier
- Choosing a PHP Web Host
- Why you will love PHP5

















There are no comments on this article yet
Please log in to post a comment about this article.