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.
FryPHP is about as lightweight as it gets.
I vote for PHP. (PHP is a templating engine.)
Which, incidentally, lets you do the following.
Should you even bother using another templating/layout engine at all, when PHP itself can suffice in a mere 6 lines?
Edit:
Perhaps I wasn't clear with how this works.
template()
is called with the name of the template (subdirectories for organization work too) with an array object as the second parameter. If the variables given aren't blank, liketemplate('index',null)
is, then the array is treated as an associative array: and every key becomes a variable containing the value.So the logic becomes:
And "views/my_template.php" is:
So, every time the variable
$oranges
is used PHP gets the data that was exported from the array,$vars['oranges']
.So all the output is then taken by
ob_get_clean()
and returned as a string. To output this string justecho
orprint
, or assign it to an array to be passed as content to the layout. If you understand this, then it is very easy to take what I've written and make a layout out of it, or pages with logic that output JSON even.I would advise you to experiment with this answer before discarding it. It has a tendency to grow on you.
Edit 2:
As requested I'll show the directory layout that my project would use. Do note that other MVC frameworks use a different structure. But, I like the simplicity of mine.
For security purposes, I have an
.htaccess
file that routes every request, except those tojs/
orcss/
, to theindex.php
script, effectively making my directories hidden. You could even make the CSS via a template if you wished, which I've done, for the use of variables, etc.So, any call made to
template('template', array())
will load the file./views/template.php
automatically. If I included a slash in the name, it becomes part of the path, like so:./views/posts/view.php
.Edit 3:
The code I've shown is a tiny excerpt of my private framework for web development. I've talked already about potentially releasing it with a dual-license, or as donation-ware for commercial use, but it's nothing that can't be written by anyone else in a short (15-21 days) time. If you want you can read my source code on GitHub... but just remember that it's still alpha material.
The license is Creative Commons SA.
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):
Then in your prepend.php file, you'll want to turn on output buffering:
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).
After this, you can write each page normally. Here's an example index.php
...and an example other.php that does something halfway interesting:
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.
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.
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,
Limonade might also be useful... not strictly layout.