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.

See it working
Download the source code