Providing front end developers reference to Smarty Vars
Working in MVC/Smarty we often have to communicate to the front end designer/developer the variables and datatypes that they can access. If not they need to crack open our PHP and locate each $smarty->assign() call to identify the template var and the datatype. Once they have located it, they usually have to analyse the data structure to see how it is formed, usually through a call to {$myTemplateVar|@print_r}.
A much simpler way of doing this is to setup a seperate “developer mode” that uses a different TPL that automatically print_r on all your template vars. This way the front end developer no longer needs to crack open your Action/ActionForm PHP code (containing your smarty->assign()) or performing print_r() through their frontend code.
When working in MVC (model2) we are treating the page actions as OO, not as procedural code (model1). The benefit of this is that our smarty->display() call is centralised to one location (our ActionDispatcher). This allows us to have control of loading a different template or inject variables for every action that is performed.
eg.
$requestURI = ‘my-page.tpl’;
// Determine the template to load
if(array_key_exists(’devmode’, $request->parameters) && $_SERVER[’REMOTE_ADDR’] == ‘127.0.0.1′) {
$smarty->assign_by_ref(’smartyVars’, $smarty->get_template_vars());
ob_start();
$smarty->display(’smarty-sniff.tpl’);
$pageBuff = ob_get_contents();
ob_end_clean();
} else {
$templateStyleController = TemplateStyler::getInstance();
ob_start();
$smarty->display(($templateStyleController->notEmpty() ? $templateStyleController->getTemplatePrefix() : “”) . $requestURI);
$pageBuff = ob_get_contents();
ob_end_clean();
}// Return our HTML to display to the client
return $pageBuff;
We can therefore maintain our TPL’s in the following manor:
tpl/template1/my-page.tpl
tpl/template2/my-page.tpl
tpl/smarty-sniff.tpl
We can control which template to load by accessing from our Singleton Class TemplateStyler which contains application logic to determine which template to load.
Our smarty-sniff.tpl contains < <<
< pre >
{$smartyVars|@print_r}
< /pre >
>>>
When a developer is developing a page, they may want to see what information is available to them for each action they are performing. Therefore, they can do so by injecting a $_GET variable “devmode” into the request:
http://localhost/mySite/?do=myPage&devmode=1
Our ActionDispatcher will identify that they wish to view the data avialable to the front end and load the smarty-sniff.tpl which will nicely print_r all their smarty information for the action being performed, quickly being able to identify how to access data in the smarty template var structure.





