I will vote for PHP. (PHP is a template engine.)
function template($file, $vars) { ob_start(); if(count($vars) > 0) { extract($vars); } include 'views/'.strtolower($file).'.php'; return ob_get_clean(); }
Which, incidentally, allows you to do the following.
echo template('layout', array( 'content' => template('page', $myData) ));
Should I use a different template / layout mechanism when PHP itself can only have 6 lines?
Edit:
Perhaps it was not clear to me how this works.
template() is called with the name of the template (subdirectories for the organization to work too) with the array object as the second parameter. If the specified variables are not empty, for example template('index',null) , then the array is considered as an associative array: and each key becomes a variable containing a value.
So, the logic becomes:
template('my_template', array( 'oranges' => 'apples' ));
And "views / my_template.php":
<html> <head> <title>Are apples == <?= $oranges ?>?</title> </head> <body> <p style="color: <?= $oranges == 'oranges' ? 'orange" : 'darkgreen' ?>"> Are apples == oranges? </p> </body> </head>
So, every time the $oranges variable 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 print this line only echo or print or assign it to an array that will be passed as content to the layout. If you understand this, then itβs very easy to take what I wrote, make a layout out of it, or pages with logic that even print JSON.
I would advise you to experiment with this answer before discarding it. He tends to grow on you.
Edit 2:
In accordance with the request, I will show the layout of the catalog that my project will use. Note that other MVC environments use a different structure. But I like my simplicity.
index.php application/ framework.php controllers/ welcome.php views/ template.php index.php
For security reasons, I have a .htaccess file that routes every request, except those related to js/ or css/ , to the index.php script, which actually hides my directories. You can even make CSS through a template if you want what I did to use variables, etc.
Thus, any call made to template('template', array()) will automatically load the ./views/template.php file. If I include a slash in the name, it will become part of the path, for example: ./views/posts/view.php .
Edit 3:
Thanks for your update. So you should have the code in the index.php file that directs the requested URL to the appropriate controller, right? 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 display controllers and / or views? What do you have in framework.php? What does it do? Thanks!
The code I showed is a tiny margin from my private web development framework. I have already talked about the potential release with a double license or as a donation for commercial use, but this is nothing that can not be written by anyone else for a short (15-21 days) time. If you want, you can read my source code on GitHub ... but just remember that this is still alpha material.
Creative Commons SA license .