views:

789

answers:

8

I'm looking for a lightweight, PHP based, layout framework. Like how the Zend Framework, uses layouts, I would like to create a layout template and include only the content for the necessary pages.

<html>
<head>
<title><?= $pageTitle ?></title>
</head>
<body>
<?= $content ?>
</body>
</html>

Anyone know of anything that does this? I would use the Zend Framework, but it's just too much for what I want to achieve.

A: 

FryPHP is about as lightweight as it gets.

Frank Farmer
A: 

BareBones: a one-file, no-configuration, MVC framework for PHP5

raspi
+20  A: 
The Wicked Flea
cool! thanks for your help!
Andrew
+1  A: 

If you want super-lightweight, you could use an auto-prepend file, mixed with some output buffering to build what you want. For starters, you need to set up your prepend and append files - put the following lines in your .htaccess file (you'll probably want to make the prepend and append files unreadable to visitors, too):

php_value auto_prepend_file prepend.php
php_value auto_append_file  append.php

Then in your prepend.php file, you'll want to turn on output buffering:

<?php
ob_start();

In append.php, you'll want to grab the contents of the output buffer, clear the buffer and do any other processing of the content you want (in this example, I set a default page title).

<?php
    if (!isset($page_title)) {
        $page_title = 'Default Page Title';
    }

    $content = ob_get_contents();
    ob_end_clean();
?>
<html>
    <head>
        <title><?php echo $page_title; ?></title>
    </head>

    <body>
        <?php echo $content ?>
    </body>
</html>

After this, you can write each page normally. Here's an example index.php

<?php
    $page_title = "Index Page!";
?>

<h1>This is the <?php echo __FILE__; ?> page</h1>

...and an example other.php that does something halfway interesting:

<?php
    $page_title = "Secondary Page!";
?>

<h1>This is the <?php echo __FILE__; ?> page</h1>
<p>enjoy some PHP...</p>

<ol>
    <?php for ($i = 1; $i <= 10; $i++) : ?>
        <li><?php echo "$i $i $i"; ?></li>
    <?php endfor ?>
</ol>

And you're done. You can grow on this a bit, such as initializing DB connection in the prepend, but at some point, you'll probably want to move to a more abstract system that breaks out of a fixed mapping of URLs to paths and files.

Sean McSomething
I like this. It's simple, uses PHP I can understand, and works the way I wanted it to. My question: Is there a way to achieve this without using htaccess? Also...I'm assuming append.php is "the layout." This assumes that I will always want to use this layout. Is there a way to do this with more than one layout? Or without a layout? I don't mind the fixed mapping of URLs, but that makes me wonder...will this work with sub-directories?
Andrew
Sean McSomething
+1  A: 

I'm actually about to release one at europaphp.org along with examples and a full documentation. It's very similar to the Zend Framework in conventions and coding standards. I'll post something when it is done; probably within the next week.

You can get the code at: [http://code.google.com/p/europa/source/browse/#svn/trunk/Sandbox - Default][1].

That link will bring up the latest svn for the Sandbox which you can just download and start using without any configuration.

Currently, it's faster than most any other PHP framework out there.

[1]: http://code.google.com/p/europa/source/browse/#svn/trunk/Sandbox - Default

Tres
Finally got around to checking out europaphp.org. Exactly what I wanted! Looks just like what I like about using Zend Framework.
Andrew
+1  A: 

Limonade might also be useful... not strictly layout.

_Kevin
+2  A: 

I've been using Smarty for ages. It's mature, actively maintained, and widely supported.

Anyway, you'll find a whole range of template engines here: http://en.wikipedia.org/wiki/Template_engine_(web)

If you click the languages column, you'll easily see what's available for PHP, and how the engines compare.

Wouter van Nifterick
-1 for smarty. Way too ancient, there are much better engines available.
Alex
Yeah Smarty isn't good - http://nosmarty.net/
raspi
A: 

Just to throw in another framework: CodeIgniter is IMHO very nice, uses an MVC approach, so you'd output your files as views and says to have a small footprint. It also has a template parser on board.

Cheers,

Boldewyn