Saving Templates to Text Files
ExpressionEngine includes a feature that will allow you to save your Template data as a regular "flat text" file on your server. This feature is useful for people who prefer to use a program such as Dreamweaver, GoLive, BBEdit, etc. to edit and make changes to their Templates (and FTP the changes to the server).
Enabling Saving Templates as Files
In order to use this feature you must first turn it on and set it up.
Prepare a directory
You must prepare a directory/folder on your server that will contain the saved Template files. Most people will create their directory inside the normal system/ directory. For example, if you want your directory named "template_files" then you would create a system/template_files/ directory on your server.
You must then make the directory writable by setting the permissions on the directory to 777 so that ExpressionEngine will be allowed to save the files as well as recreate your Template_Group/Template structure.
Set Control Panel Preferences
Once your directory is created and permissions are set correctly, you can log into your Control Panel. Go to Templates > Global Template Preferences. There are two preferences you need to set.
- Allow templates to be saved as files? Set this to "Yes".
- Basepath to Template File Directory: You must place the server path to the directory you created above in this preference.
It is important that you use the server path for the preference and not a URL. A server path often looks similar to /home/usr/domain.com/http_docs/system/template_files/. Server paths will vary from server to server, so you should contact your Host or server admin if you are unsure of what your setting should be.
Running your Site with Flat File Templates
Using the Feature
Once you have set things up as indicated above, you will then see a new checkbox become available at the bottom of your Template editing page. When you submit your template with the box checked, the Template will be saved as a file (in addition to updating the database). It will also tell ExpressionEngine to use the file version of the Template instead of the database whenever your pages are viewed. Because ExpressionEngine is using the file version, you can then use an external program such as Dreamweaver, GoLive, BBEdit, etc. to make changes to the file without using the Control Panel interface.
Note: If you have been making changes to your template files via FTP, and you then go back to the Template editing page, ExpressionEngine will load the file version into the form. However, unless you click "save", the database will still contain the last version you saved via the Control Panel.
Saved Template Files
When you save your Template to a file, the file is saved inside the directory that you created above. Files are saved according to your Template Group/Template structure. That means if you save the "comments" Template inside the "weblog" Template Group, ExpressionEngine will create a saved Template file similar to this: system/template_files/weblog/comments.php.
Saved Templates and the Multiple Site Manager
Sites under the Multiple Site Manager have their own preferences for saving templates as text files. As such, when setting up a new Site under the Multiple Site Manager, you must set up a new location for saving those templates.
When importing or duplicating to a new Site in the Multiple Site Manager, if you wish to use existing templates which are saved as text files, then the template directory must be manually copied to the directory for the new Site.
User Contributed Notes
If you want your System Offline Template to have the same look and feel as your site then use this method to save your CSS file and add a link in the System Offline Template to it:
<link rel=“stylesheet” type=“text/css” media=“screen” href=”/system/templates/pages/main_css.php” />
Note the file is saved as a php but this does not seem to matter.
An additional caveat: I’ve been bitten by this recently as I was migrating files around. I created several new templates based on existing templates, saved them as files, then deleted the original ones. Bad! This creates a new template, of course, based on the current version in the database, not the file. I inadvertently blew away days of work this way.
Perhaps a feature request in this department could be submitted, specifying a need for a batch ‘update template files to db’ type of function…
Here’s a gotcha: “...unless you click “save”, the database will still contain the last version you saved via the Control Panel.”
This means that though your live site will display the latest edits you’ve made, the template search (searching from the small box on the templates page) won’t search the current version of any template you’ve edited as a file. Instead it’ll search through the old version of the template that you saved via the administrative control panel.
Editing templates as files also goes around the template revision system, so keep your own backups as you go.
You must have an ExpressionEngine license and have attained a forum rank of "Lab Assistant" (100 posts) to contribute notes to the User Guide