News Feed
Sections




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

Rasmus Larsson:
Building a template engine in PHP
May 31, 2013 @ 11:11:27

Rasmus Larsson has a recent post to his site showing how to build a basic templating engine in PHP that uses the "__toString" to help render the result.

Possibly the most common sign of bad code is tight coupling, especially between logic and presentation. It might seem like a good idea to print out the HTML while the data is being generated, but it more often than not leads to a big incoherent mess of tangled code. [...] While PHP makes it unnecessarily easy to write shitty code it also provides a lot of ways to avoid it. I'll use this post to show you how ridiculously easy it is to create a template engine in PHP in three easy steps.

He includes the sample code for a "Template" class and shows the combination of exporting variables and output buffering to return the resulting template populated with values. The "__toString" method makes it so that you can just echo out the object and get the results.

0 comments voice your opinion now!
template engine tutorial simple tostring view

Link: http://www.rasmuslarsson.se/2013/05/a-template-engine-in-php

blog comments powered by Disqus

Similar Posts

Tim Koschuetzki's Blog: How To Transform HTML To Textile Markup - The CakePHP TextileHelper Revisite

PHPBuilder.com: Smarty PHP Template Engine: Building PHP Apps in a Flash

Oracle Technology Network: DeployPHP Series, Part 5 - Zend Core for Oracle Quick Start

Alvaro Videla's Blog: Erlang as Session Storage for PHP

Brian Swan's Blog: Using the Windows Azure Command Line Tools for PHP


Community Events





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


artisanfiles series podcast language symfony laravel introduction list opinion interview security version composer community tool release library conference framework voicesoftheelephpant

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