Jump to content

Template Installation


phpSensei

Recommended Posts

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? :P

 

All help is appreciated,

 

~Sensei

Link to comment
Share on other sites

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();
}
}
?>

Link to comment
Share on other sites

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. :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.