Extending Smarty / Smarty_Compiler : some thoughts + Patch

From: Date: Thu, 01 Aug 2002 23:31:08 +0000
Subject: Extending Smarty / Smarty_Compiler : some thoughts + Patch
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5543@lists.php.net to get a copy of this message
Hello, Here is a problem I had and a patch to solve it. I have extended the Smarty class to add a variable : ( $myvar ) as follows : ---------------------------------------------------------------------------------------- require_once( SMARTY_DIR . "Smarty.class.php"); class MySmarty extends Smarty { var $myvar = 'default'; /*======================================================================*\ Function: MySmarty Purpose: Constructor \*======================================================================*/ function MySmarty() { // call constructor $this->Smarty(); } function change_var( $param ) { $this->myvar = $param; } } ---------------------------------------------------------------------------------------- And what I'd like to do is to create a postfilter that will write the content of this variable at the start of the page (for example. In fact I have more variables, and doing more obscure compile-time things.) ---------------------------------------------------------------------------------------- function add_comment($tpl_source, &$smarty) { return "<?php echo \"<!-- MyVariable : " . $smarty->myvar . " -->\n\" ?>\n".$tpl_source; } ---------------------------------------------------------------------------------------- However, this can not work, because the $smarty object passed to the postfilters is an instance of Smarty_Compiler, not an instance of Smarty. And, even if Smarty_Compiler inherits Smarty, it doesn't inherit my extended class, so it doesn't know about the *existence* of the new variable (and, even if it knew about the existence, it wouldn't have its *value*, because when instanciating Smarty_Compiler, Smarty does the job of filling all the values of the variables in the newly created instance, to be in synch with Smarty) What I can do is : A) Modify Smarty source code (that's not fun, because at each new Smarty version, I have to do it again. Ok, that's what I do, and I use CVS, but this isn't a beautiful way of doing it) B) Inheritance (the preferred way, I think) 1) Extends Smarty_Compiler (inheriting Smarty, and adding the new variables... we cannot do multiple inheritance in PHP ? =) ) 2) overload Smarty's _compile_template, to initialize this new variable before doing the work. Due to the nature of the function, I'll have to copy/paste the code, and whenever a new version of Smart comes out that change this function, I'll have some troubles. Either way, I'll have to monitor Smarty for source code changes at each new version, which is not cool. I think there may be a solution : taking out of Smarty's _compile_template all the initialisation code and putting it in the compiler class, in a "setup( &$smarty )" function that will be called by _compile_template just after the class instanciation. That way, I'll be able, in my extension of Smarty_Compiler, to overload this 'setup' function to add the new variable initialisation. It seems complex, but here is a two-file proof-of-concept patch that shows how it could be done. [ it's not clean, it may be difficult to apply the patch because my Smarty is already heavily modified :o(( ] (By the way, in the patch of Smarty.class.php, you can see a modification of the code to locate the new compiler_class, because it wouldn't work with my file outside of Smarty's directory. Obviously, my modification is a quick and dirty hack, because it prevents the real Smarty_Compiler to be found. There is something that needs to be done here.) After the patch, I can do the following : ---------------------------------------------------------------------------------------- require_once( SMARTY_DIR . "Smarty.class.php"); class MySmarty extends Smarty { var $myvar = 'default'; /*======================================================================*\ Function: MySmarty Purpose: Constructor \*======================================================================*/ function MySmarty() { // call constructor $this->Smarty(); $this->compiler_class = "MySmarty_Compiler"; } function change_var( $param ) { $this->myvar = $param; } } ---------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------- require_once( SMARTY_DIR . "Smarty_Compiler.class.php"); class MySmarty_Compiler extends Smarty_Compiler { var $myvar; function setup( &$smarty ) { Smarty_Compiler::setup( &$smarty ); $this->myvar = $smarty->myvar; } } ---------------------------------------------------------------------------------------- And having my postfilter working as expected, and, more generally, having in my extended 'Compiler' class, access to all variables of my extended 'Smarty' class. I hope this can help others having the same problems, and may be it will have enough interest to make it in the CVS. Regards, Ludovic LANGE.

« previous php.smarty.general (#5543) next »