Lightweight, PHP-based layout structure ... do you know anything?

I am looking for a lightweight, PHP based layout layout. Just as the Zend Framework uses layouts, I would like to create a layout template and include only content for the necessary pages.

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

Does anyone know anything that does this? I would use the Zend Framework, but this is too much for what I want to achieve.

+4
source share
8 answers

I am going to release it on europaphp.org along with examples and full documentation. It is very similar to the Zend Framework in conventions and coding standards. I will write something when this is done; probably over the next week.

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

This link will open the latest svn for Sandbox, which you can simply download and start using without any configuration.

Currently, it is faster than most other PHP frameworks.

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

+2
source

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 .

+33
source

If you want super light weight, you can use an auto-preend file mixed with some output buffering to build what you want. First you need to configure your files to add and add files - put the following lines in your .htaccess file (you probably want to also make the files for adding and adding files unreadable to visitors):

 php_value auto_prepend_file prepend.php php_value auto_append_file append.php 

Then in the prepend.php file, you want to enable output buffering:

 <?php ob_start(); 

In append.php, you want to capture the contents of the output buffer, flush the buffer, and do any other processing on the content you want (in this example, I set the 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 that, you can write each page as usual. Here is 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 half 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, for example, initialize a database connection in preend, but at some point you probably want to move on to a more abstract system that breaks out from a fixed mapping of URLs to paths and files.

+3
source

BareBones : single-phase, no configuration, MVC infrastructure for PHP5

+2
source

FryPHP is about as light as he is.

+1
source

Limonade can also be useful ... not strictly a layout.

+1
source

I've been using Smarty for ages. He is mature, actively supported and widely supported.

In any case, here you will find a number of template engines: http://en.wikipedia.org/wiki/Template_engine_(web)

If you click on the language column, you can easily see what is available for PHP and how the engines are compared.

+1
source

Just throw another structure: CodeIgniter IMHO is very beautiful, uses the MVC approach, so you will output your files as looks and says to have a small footprint. It also contains a template parser on board.

Greetings

+1
source

Source: https://habr.com/ru/post/1285777/


All Articles