I am using the Smarty Template engine (http://smarty.net) on a website but I have additional, custom Functions to handle template compiling tasks.
In order to be able to update the Smarty framework files (e.g. /smarty/Smarty.class.php) and not having to copy-paste my custom function into the Class inside Smarty.class.php, I thought "hey, let's just make my own Class and extend the Smarty Class!". However, I can't get this to work and would appreciate any enlightening advice :)
This is what I got at the moment:
- Web-Root/
- includes/
- Smarty.inc.php
- smartylib/
- Smarty.class.php
- Smarty_Compiler.class.php
- includes/
Smarty.class.php
The third party vendor file and class that I don't wanna touch:
class Smarty {
// various vars declarations
public function __construct() {
// ...
}
function _compile_source($resource_name, &$source_content, &$compiled_content, $cache_include_path=null) {
// magic...
$smarty_compiler = new $this->compiler_class;
// more magic...
}
// more class methods declarations
}
Smarty_Compiler.class.php
Another third party vendor file and class that I don't wanna touch:
class Smarty_Compiler extends Smarty {
// various vars and methods declarations...
function _syntax_error($error_msg, $error_type = E_USER_ERROR, $file=null, $line=null) {
$this->_trigger_fatal_error("syntax error: $error_msg", $this->_current_file, $this->_current_line_no, $file, $line, $error_type);
}
// more various methods declarations...
}
Smarty.inc.php
My custom Smarty include file with new Subclasses and instantiating the $smarty object:
/**
* My extensions for the Smarty Class
*/
Class MySmarty extends Smarty {
// different custom vars declarations
/**
* My ADDITIONAL custom Template Compile function
*/
public function compile ($template, &$errors) {
// custom code
}
}
/**
* My extensions for the Smarty_Compiler Class
*/
Class MySmarty_Compiler extends Smarty {
// different custom vars declarations
var $_manual_compiler_errors = array();
/**
* My REPLACEMENT _syntax_error function
*/
public function _syntax_error($error_msg, $error_type = E_USER_ERROR, $file=null, $line=null) {
global $_manual_compiler_errors;
if ($_manual_compiler_errors) {
array_push($_manual_compiler_errors, "smarty syntax error on line ".$this->_current_line_no.": $error_msg");
}else{
$this->_trigger_fatal_error("smarty syntax error: $error_msg", $this->_current_file, $this->_current_line_no, $file, $line, $error_type);
}
}
}
// Instantiate Smarty
include_once($_SERVER['DOCUMENT_ROOT'].'/smartylib/Smarty.class.php');
$smarty = new Smarty;
$smarty->debugging = true;
$smarty->force_compile = false;
Result and problem
Okay I hope my intentions are clear from above code snippets: I want the object $smarty to contain also
- my additional, new custom template function,
- and my replacement function
And because I use "extends" for my 2 classes declaration, I thought this should simply work by using:
$smarty->compile(...);
$smarty->_syntax_error(...);
But unfortunately it doesn't :( I COULD add my custom functions directly into the Smarty Class inside Smarty.class.php - but this will, obviously, make the file non-updateable.
What am I missing by using my 2 Subclasses extending the Smarty Class and while talking to the methods declared in them? Or how would you approach extending a third-party Class with custom/rewritten functions without touching the original code?
Thank you for any advice!