How I PHP: The Output Handler – Continued
Last time, after write the ‘How I PHP: The Output Handler’ article, David Arnold asked a question about adding some dynamic data in your template. For instance, it would be nice if the data in the left column, ‘Beauty Tips’, could be different for each page. There are several ways to solve this, but for know I will choose the simplest.
We’ll start with making changing template file, adding markers for the title and for the left menu.
[snip php-source]code/output_handler_2/obdemo/template.html[/snip]
Next we need to change the OutputHandler class. We need to either add a singleton method or make the class static, so we can use it in our code. I’ve chosen to make the class static in this example. We’ll add a $data property and a setData() method, so we can set data for markers other than content.
[snip php-source]code/output_handler_2/obdemo/init-step1.php[/snip]
We can use the setData() method to set the title of the page.
[snip php-source]code/output_handler_2/obdemo/portfolio-step1.php[/snip]
We could also use the same method to add the data for the left column. However, putting HTML in a php string isn’t really the way we want to go. We just want to output the content and use that. The ob_start ob_get_contents can help us here. Let’s use this in the OutputHandler class.
[snip php-source]code/output_handler_2/obdemo/init.php[/snip]
Now we can simple use the mark() method to specify a section within the template. With endmark() we specify that we’re outputting content data again.
[snip php-source]code/output_handler_2/obdemo/portfolio.php[/snip]
That’s it. You can take this much further. However, if it all gets to dynamic you’ll loose the whole reason of doing it like this. The idea is to use an output handler to add static data as dynamic pages, with static as a keyword.
05 Dec 2007 Arnold Daniels




This is pretty neat, I like the idea behind it. Hopefully I will have some time to try this for a larger scale demo and see how it helps the flow of things. Also, When you say your name “Arnold” do people relate it to “Arnold Swarchenegger”? It happens to me all the time
David: It would be nice to see this in a larger scale demo. I agree. Interesting approach nonetheless.
This is used it in the new MySQL UDF site (http://mysqludf.com), the Javeline site (http://www.javeline.com), the Javeline developer center (http://developer.javeline.net) as well as dozens of sites and web applications made by Bean IT, my former employer.
For me, this works really great. So give it a try if you can.
Yeah Mr, Swarchenegger
all the time indeed.
PS. Would anybody find it interesting to see an article on how to use the referrer to add a custom piece to the site based on where the visitor came from? I have an example of this when click on this link. You’ll see an article about my work at Javeline and a picture of me. When you just type in ‘developer.javeline.net’ in the browser, you will not see that article.
it’s a really cool idea but i’m having a hard time thinking of many practical uses (besides a referral system =0) If it makes you feel better I will read it and use it in at least one project (i’ll think of something) if you post it.