How you can create template/theme system and wordpress plugin system for any custom Content management systems much like Wordpress or joomla?

Edit:

I'd rather not use any templating engine much like wordpress grounds why it is known for templating.

$varArray = array('firstVar' => 'the value', 'secondVar' => 255);
$varArray['theThirdVar'] = 12345;
foreach($varArray as $name => $value){
    $$name = $value;
}
include ('templates/template.php');

within the template.php then make a move like:

<?echo $firstVar;?>

this then should provide you with the Value

EDIT: This can be a minimal example. Additionally you could have a look into codeigniter source. Additionally they make use of this kindo of Template System.

if you are searching to construct a custom Content management systems why not choose a php framework and develop surface of it. Why build on your own whenever you an begin with an established and examined framework. Codeigniter is dead easy and Yii has built a wordpress plugin system into its framework via components and modules: http://www.yiiframework.com/doc/guide/basics.module

Unless of course you are just learning on your own, there is no help to building on your own.