Page 1 of 1

Automatic Templating in PHP

Posted: Sun Aug 15, 2010 4:23 am
by Jackolantern
I came across this nifty feature in PHP the other day, and thought I would share. It is particularly helpful for PHP web games, which often have to break out of a page with exit(), therefore breaking the rest of the page from being displayed unless you make complicated use of include files or make template functions. The actual feature lives in the php.ini file. The actual values are:

Code: Select all

auto_prepend_file
...and...

Code: Select all

auto_append_file
What these do is they let you specific the path of a file that will automatically be included before your code (prepend) and/or after your code (append). The most common use of this feature is to add your website header and footer into the respective file paths so that your static page header is added to the prepend path and your footer is added to the append path. That way, you can exit() from scripts or do any kind of logic you need to without worrying about whether or not the rest of your page is going to be displayed.

Typical usage on Windows would look something like this:

Code: Select all

auto_prepend_file = "c:/wamp/www/test/headertemplate.php"
(Use the auto_append_file path in the same way to have the contents of the file added to the end of all your pages)

Typical usage on Linux would look like this:

Code: Select all

auto_prepend_file = “/home/username/headertemplate.php”
The major downside to this is that I don't believe there is a way to set it on a page-by-page basis. However, that is also one of the positives about it. You give up some flexibility for complete convenience for the display of your website template, and it almost functions in the same way as ASP.NET's "Master Pages" feature where you only have to write and deal with "content areas" and not with the base template. The template itself is handled for you.

I hope some people find this as helpful as I believe it will be for me :)

Re: Automatic Templating in PHP

Posted: Sun Aug 15, 2010 8:31 am
by Chris
Nice find! Thanks for this one Jack! Can you also make it do more files than one?

Re: Automatic Templating in PHP

Posted: Sun Aug 15, 2010 4:43 pm
by Jackolantern
Unfortunately I didn't see a way to use more than one file at a time, outside of the fact that prepend and append can use two different files. Maybe there would be a way to configure Linux to only apply a particular php.ini setting to a specific folder, which at least would allow you to have multiple websites on the same server using this feature (however, that is past my ability in Linux). I will try to dig around and see if I can't find some more options for this!

Re: Automatic Templating in PHP

Posted: Mon Aug 16, 2010 7:10 pm
by hallsofvallhalla
very nice

Re: Automatic Templating in PHP

Posted: Mon Aug 16, 2010 9:12 pm
by Jackolantern
After thinking about it some more, I came to the conclusion that you could just append multiple files together manually with copy'n'paste and then use the php.ini setting to append that combo-file automatically. Since all it does is add the contents of a file at the bottom and/or top of your content files it would be the same thing, and not much extra work since you only have to do it once for the whole site :)