Archive for the ‘hacks’ Category

So you want PHP Templates, eh?

September 2nd, 2009 No comments

PHP Templates! Everyone wants them, some people try to provide them to others, but deep down inside, you all know smarty isn’t the way to go… Allow me to show you a better way.

Just use PHP! Yes, that’s right! PHP is almost designed from the ground up natively support templates!

Here’s the secret… When you include a php file (using the include(“somefile.php”) method, the php code inside the included file has the same variable scope as the code block that included it… So… Take the following two files….

  function go(){
    $a = "Hi There!";
<?php echo $a; ?>

The output would be:

Hi There!

If you’re a noob, another name for this (if you make extra effort to not put anything but loops, and echos in your template) is MVC (Model View Controller). While it isn’t a complete implementation, it’s a really really good start and makes writing and maintaining a site WAY easier. Cake PHP uses this approach for it’s views (aka templates) as well. Java uses this, but instead of the “view” using the local variables where a page is displayed, the data you want the view (or jsp page in java) to have access to are put in the request object as an “attribute”.

I, personally, put the variables I want to use in a “template” (aka “view”) in a global array ($GLOBALS[‘params’][‘name’] = “bob”;) It isn’t the best practice, because some part of your code far away could write to this and mess something up, but for simple sites, I find it quick and easy.

Of course, if you REALLY want a templating system, you could use SMARTY. (Blech!) But, I really recommend you just get used the to the echo statements and use php to render templates.

So, some sample code from a site of mine is:

require_once(BASE_FS_PATH . '/classes/BookService.php');
	case 'Research':
		$book = BookService::importBook($_POST['ean']);
		$_GLOBALS['params']['book'] = $book;
		include(BASE_FS_PATH . '/views/pages/research.php');	
	case 'mark_not_sellable':
		$book = BookService::importBook($_GET['ean']);
		$book->sellable = 'no';
		$_GLOBALS['params']['msg'] = 'Book Marked As Not Sellable.';
	case 'mark_not_available':
		$book = BookService::importBook($_GET['ean']);
		$book->available = 'no';
		$_GLOBALS['params']['msg'] = 'Book Marked As Not Available.';
	case 'details':
		$book = BookService::importBook($_GET['ean']);
		include(BASE_FS_PATH . '/views/pages/research.php');
function show_details($book){
	$_GLOBALS['params']['book'] = $book;
	include(BASE_FS_PATH . '/views/pages/details.php');

And the “view” for this code (details.php) is:

<body style="width:950px; margin: 0 auto; text-align: left;" class=" yui-skin-sam">
			include(BASE_FS_PATH . '/views/pieces/menu_and_js.php');
		<div style="text-align: center;">
			<?php echo $_GLOBALS['params']['msg']; ?>
			<h1><?php echo $_GLOBALS['params']['book']->title; ?></h2>
			<li>Ean: <?php echo $_GLOBALS['params']['book']->ean; ?></li>
			<li>Available: <?php echo $_GLOBALS['params']['book']->available; ?></li>
			<li>Imported: <?php echo $_GLOBALS['params']['book']->import_date; ?></li>
			<li>Cost: <?php echo $_GLOBALS['params']['book']->cost; ?></li>
			<li>Change In Inventory: <?php echo $_GLOBALS['params']['book']->change_in_inventory; ?></li>
                         (More Code That's been removed for this example)

Anyways, there are LOTs of templating engines out there you can use instead, but I would discourage it. This is the fastest, simplest, most powerful way to do templates (IMO).

Categories: 100 Weeks of PHP, hacks, php5 Tags:

min-height CSS Hack

July 17th, 2009 No comments

I needed a min-height hack for css that worked everywhere. I searched for a while, and found this link: It works as advertised, just checkout the tabs on

Categories: css, hacks, html Tags: