Edit report at http://bugs.php.net/bug.php?id=51332&edit=1

 ID:          51332
 Updated by:  johan...@php.net
 Reported by: robert dot frunzke at gmail dot com
 Summary:     Context objects replacing "template engines"
-Status:      Open
+Status:      Wont fix
 Type:        Feature/Change Request
 Package:     Scripting Engine problem
 PHP Version: Irrelevant

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2010-03-19 14:40:24] 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.





------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=51332&edit=1

Reply via email to