I vote for PHP. (PHP is a templating engine.)
function template($file, $vars) {
ob_start();
if(count($vars) > 0) { extract($vars); }
include 'views/'.strtolower($file).'.php';
return ob_get_clean();
}
Which, incidentally, lets you do the following.
echo template('layout', array( 'content' => template('page', $myData) ));
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, like template('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:
template('my_template', array(
'oranges' => 'apples'
));
And "views/my_template.php" is:
Are apples == = $oranges ?>?
">
Are apples == oranges?
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 just echo
or print
, 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.
index.php
application/
framework.php
controllers/
welcome.php
views/
template.php
index.php
For security purposes, I have an .htaccess
file that routes every request, except those to js/
or css/
, to the index.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:
thanks for your update. So you must have some code in your index.php file that routes the requested url to the appropriate controller, correct? Could you show some of this? Also, it doesn't look like your views mirror your controller directory. Can you explain a little more how urls map to controllers and/or views? What do you have in framework.php? What does it do? Thanks!
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.