php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51332 Context objects replacing "template engines"
Submitted: 2010-03-19 14:40 UTC Modified: 2010-03-21 19:22 UTC
From: robert dot frunzke at gmail dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: robert dot frunzke at gmail dot com
New email:
PHP Version: OS:

 

 [2010-03-19 14:40 UTC] robert dot frunzke at gmail dot com
Description:
------------
PHP once was a kind of template engine, nowadays it is much more and we use or write our own template engines to ensure separation of code and layout (or data). Its a strange evolution, that most template engines "compile" their language back to PHP for faster execution.

So, why can't we just write templates in PHP? We can, but it requires discipline, because template writers could start programming and could start using functionality that was not meant for them.

Here is a suggestion, which involves the PHP core, but would have many uses.

We invent contexts. A context can be created, configured and a single or a list of php files can be executed through them.

Example:

$ctx = new context();
$ctx->add_class( "some_class" ); // makes class visible in context
$ctx->add_global( "some_global", $value_of_some_global );
$ctx->add_function( "some_global_function" );
$ctx->enable_builtin( "strlen" );
$ctx->enable_builtin( "strtolower" );
$ctx->enable_builtin( "strtoupper" );
$ctx->enable_super_global( "_REQUEST" );

// short_tags make a lot of sense in template-like scripts
$ctx->enable_short_tags( true );

// execute and return output as a string (avoid output buffer fiddling)
$output = $ctx->execute_and_return_output( "some_script.php" );

// execute, like include/require
$return_value = $ctx->execute( "some_script.php" );


A script executed in a context has only limited functionality, it can use all language constructs, but NO built-in functions, and does not see classes, functions, globals and so on. Any functionality must be explicitely enabled on the context object!

Of course this will require some work on the PHP core, but IMHO it is worth it. Template engines can be thrown away, template caches (which usually are "compiled" to PHP code) and all its problems (including security-related problems) can become a thing of the past, and PHP could be used for templating again, but in a safe manner this time.

If people agree and like this feature, I would love to help on the development on the PHP core. Maybe this can even become a PHP 6 feature, maybe PHP 7.



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-21 19:22 UTC] johannes@php.net
-Status: Open +Status: Wont fix
 [2010-03-21 19:22 UTC] johannes@php.net
The runkit extension in PECL provides a similar functionality. for having this a core part of the language would require a major rewrite of the architecture and is most likely not worth the effort.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 07:01:28 2024 UTC