phpSensei Posted August 19, 2008 Share Posted August 19, 2008 Hello, I need some php "help" here so I was wondering what do you think is the best way to approach this method of programming, and how would you go about making it more "facile". In other words like a forum software template you download them and install them, and next thing you know every page is updated. I am in the process of created a eCommerce Software Solution, and I want my users to be able to download templates and install them. I have done this before but I always find my method too messy, and I add too many str_replace functions everywhere. any idea/s? All help is appreciated, ~Sensei Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted August 19, 2008 Share Posted August 19, 2008 There are a lot of template engines out there like smarty, etc. This is what I use... you can ignore the sections of code about addons. Those are just how I handle classes used in the template. I'd cut them out myself, but I would think they might help with understanding how to do it. As for the GlobalFuncs you see thrown in there, makeArray() forces something to be an array (removing some quirks typecasting causes), and loadAddon() just includes and returns an instance of the passed addon. <?php class Template { public $file; public $variables = array(); public $addons = array(); /* * Set template file to be used and the addons */ public function __construct($fileName = '', $addons = array()) { $this->addons = GlobalFunc::makeArray($addons); if(empty($fileName)) { $this->file = APP . '/views/master.tpl.php'; } else { $this->file = $fileName; } } /* * Set a variable to be extracted later */ public function set($variable, $value) { $this->variables[$variable] = $value; } /* * Used by other functions to load files with defined variables and addons */ private function loadFile($file) { // Load some variables extract($this->variables); // Load the addons $addons = array(); foreach ($this->addons as $addon) { $addons[$addon] = GlobalFunc::loadAddon($addon); } extract($addons); // Load the file ob_start(); include($file); return ob_get_clean(); } /* * Loads a file into a variable */ public function FileToVar($variable, $file) { $this->variables[$variable] = $this->loadFile($file); } /* * Return the rendered final product */ public function render() { ob_start(); echo $this->loadFile($this->file); return ob_get_clean(); } } ?> Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 19, 2008 Author Share Posted August 19, 2008 Thanks for the reply, this method isn't as flexible as I imagined, but I think I got what you ment. I probably will approach another method but thats not for sure, I'll credit you for the help once I finish the software with some other programmers. Been working on it for 4 months so far... PM me your name if you want to be included for the support section. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.