Willowgarden Highlight: Template Modes
Welcome to the first post in a series of posts I'll be writing about highlights of the Willowgarden application platform for PHP 5. This first highlight may be a simple one, but it's a very useful one as you will shortly find out.
Willowgarden, like many Web frameworks, is built around the concept of pages and actions. A page such as "photo" might have actions such as "view", "edit", "remove", etc. Some actions are visual and don't alter content, and some actions do alter content and aren't visual (they redirect to a visual action). The visual actions, of course, need to output HTML to the browser. How does that work? Well, as you might think, every page has a template associated with it, which you use via the page's built-in renderer to render the template. But, unlike many Web frameworks, Willowgarden doesn't provide a different template for every action. All actions are provided via a single template file for the entire page. This works because of the concept of template modes. (Note: for the purposes of this article, we're assuming we're using the default PHP-based template engine.)
Say someone wants to edit information for a photo. They'll go to a URL such as http://www.mysite.com/photos/edit/12345, which means the edit action of the photos page will be called and the first path parameter will be 12345. Now obviously you only want the HTML for the edit action to be rendered; you don't want the HTML for the view action or the process action or whatever to be rendered. So in the template file ("photo.php.tpl"), you would do this:
-
<?php
-
if ($this->getMode() == "view"):
-
?>
-
<p>Hello. This is the view action template.</p>
-
<?php
-
endif;
-
if ($this->getMode() == "edit"):
-
?>
-
<p>Hello. This is the edit action template.</p>
-
<?php
-
endif;
-
// and so on and so forth
-
?>
Not bad, eh? Of course, the tradeoff of more files that are simple vs. fewer files that are slightly more complex might seem like a draw. But, and this is a big but, this template isn't in and of itself action-specific. So you can do tricks like this:
Aha! Now we're handling two different template modes in one pass. We can have specific bits of the template for either action output different HTML based on the mode but keep much of the HTML the same. This is very helpful when we deal with "add" vs. "edit" -- 90% of the page content might be the same in either case. With sophisticated use of template modes, you can keep 90% of that HTML in one single place. Bottom line: managing your HTML templates just got a whole lot easier.
I hope you enjoyed my first Willowgarden Highlight. Come back soon for #2!

Del.icio.us
Cosmos
Digg
September 13th, 2006 at 4:30 am
Jared White’s Blog: Willowgarden Highlight: Template Modes…
…