News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

DevShed:
Using Closures as View Helpers
January 25, 2012 @ 09:50:38

New on DevShed today there's a tutorial looking at using one of the newer features of PHP, closures, as view helpers in a basic templating system.

In this two-part tutorial I'll be showing you, in a step-by-step fashion, how to use the goodies offered by closures in the implementation of an object-based, easily extendable template system. This system will allow you to embed anonymous functions easily into template files, and call them as typical view helpers, too.

He starts the process of creating the templating system by defining two interfaces, the View and DataHandler. Using these as a base, he creates an instance of the ViewInterface (a "View" class) that can set the template file to use, set values to be displayed and render the formatted output. Included is a basic template and how to use the View class to set values into it. The "render" method is called on the view and the HTML markup is produced. The closure comes in when they try to call a value "clientIp" that needs to do something more complicated than just having a string assigned to it.

0 comments voice your opinion now!
closure view helper template view interface


blog comments powered by Disqus

Similar Posts

Brian Moon's Blog: PHP Template tests (and Blitz)

YoungCoders.com: Debunking the Bloated Smarty Myth

International PHP Magazine: Three Releases - Graphing, Blitz and Plugins

DevShed: Developing an Extensible Template Processor in PHP 5 (Part 1)

Dave Dash's Blog: Saving a file to a database using symfony and doctrine


Community Events





Don't see your event here?
Let us know!


bugfix laravel list series podcast release symfony community deployment introduction voicesoftheelephpant language framework threedevsandamaybe interview zendserver conference tips api code

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework