Using Object Oriented Programming With Smarty

Object Oriented Programming With Smarty

Using OOP with the Smarty template engine can be done with relative ease; especially if you have a decent understanding of OOP to begin with. If words like scope, public, private, constructor, inherit, class, methods and properties do not strike a chord, then, you may want to strengthen your OOP skills before you go one step further and adapt them to Smarty templates.

Now for the fun part. Let's create some classes within the '.php' file and make them output as expected using Smarty's advanced features. Again, like previous examples, it is easier if you can see the codes and test your pages with your browser. The two files are shown below.

.php File

Here is the file which does the heavy lifting. The file starts with the usual instantiation of the Smarty class. Then, there are the two classes; Tag and its child class called Form. Underneath both classes, an object for each class was instantiated using the new() operator. For example, the Form class was instantiated with $test = new Form('one','two','');.

After that, the assignByRef() was used to allow us to assign an objects named my_object and my_tag. The second parameter contains the recent object that was created. In our example, the second parameters are $test and $test2. Unlike OOP without Smarty, the $test object now will be used with the '.tpl' file under the new name $my_object.

So, if you are to use pure PHP, you would just write $test->be('add input here') whereas in the .tpl file it will look like $my_object->be('add input here').

Change the return value form the be() method to see your various values which you can output.

<?php include_once("libs/smarty.class.php"); 
$smarty = new smarty();  

class Tag {      
public $a = "test";     
public $b = "test2";     
public $d = "testd";      

function __construct($a, $b) {              
}          

public function fromparent(){         
echo "hey public";     
}          

private function fromprivateparent(){         
echo "hey private";     
}           

protected function fromprotectedparent(){         
echo "hey protected";     
}  
}  

class Form extends Tag {      
function __construct($a, $b) {         
parent::__construct($a, $b); 	
$this->a = $a;         
$this->b = $b; 	
} 	 	
public function be($c) {         
//return $this->b;           
//return $this->d;		 	  
return $c;     } 
} 	 

$test = new Form('one','two','');  
$test2 = new Tag('required','also required');  
$smarty->assignByRef('my_object', $test); 
$smarty->assignByRef('my_tag', $test2); 
$smarty->display("object.tpl");


.tpl File

Here is the file which will output data from the classes. As you can see, they use special tags. The tags below will output two methods from the two classes.

 <html> 
<head> 
<title>Job Application Form</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
<body>  
{* output assigned object *} 
{$my_object->be('from template')} <br/> 
{$my_tag->fromparent()}  
</body> </html>