If you don't want to provide the template code directly inside the Main file, you can use this Template Manager to handle that code outside the main file.
class TemplateManager {
protected $basePath;
protected $currentTemplate;
protected $currentTPLFile;
public function __construct(string $basepath, $currentTemplate = "admin/home.tpl"){
$this->setBasePath($basepath . '/Templates/');
$this->setCurrentTemplate($this->getBasePath() . $currentTemplate);
$this->setCurrentTPLFile($currentTemplate);
}
public function getTemplate($page, $isAdmin = false){
return ($isAdmin ? $this->getBasePath() ."admin/" . $page .".tpl" : "Templates/client/" . $page . ".tpl");
}
/**
* Get the value of currentTemplate
*/
public function getCurrentTemplate()
{
return $this->currentTemplate;
}
/**
* Set the value of currentTemplate
*
* @return self
*/
public function setCurrentTemplate($currentTemplate)
{
$this->currentTemplate = $currentTemplate;
return $this;
}
/**
* Get the value of basePath
*/
public function getBasePath()
{
return $this->basePath;
}
/**
* Set the value of basePath
*
* @return self
*/
public function setBasePath($basePath)
{
$this->basePath = $basePath;
return $this;
}
/**
* Get the value of currentTPLFile
*/
public function getCurrentTPLFile()
{
return $this->currentTPLFile;
}
/**
* Set the value of currentTPLFile
*
* @return self
*/
public function setCurrentTPLFile($currentTPLFile)
{
$this->currentTPLFile = $currentTPLFile;
return $this;
}
}
function module_output($vars) {
$smarty = new Smarty();
$_lang = $vars['_lang'];
$TPLManager = new TemplateManager(dirname(__FILE__), "admin/home.tpl");
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
$smarty->assign('modulelink', $vars['modulelink'] );
$smarty->caching = false; // Debugging
$smarty->compile_dir = $GLOBALS['templates_compiledir'];
$CustomFunction = "template_" . $page;
$smarty->assign('tplVars', (new TemplateService)->$CustomFunction($vars));
//Load Template
$smarty->display($TPLManager->getTemplate($page, true));
}
function module_clientarea($vars){
(...)
$TPLManager = new TemplateManager(dirname(__FILE__), "admin/home.tpl");
return array(
'pagetitle' => 'Page Title',
'templatefile' => $TPLManager->getTemplate($page),
'requirelogin' => true, # accepts true/false
'forcessl' => true, # accepts true/false
'vars' => array(
'modulelink' => $modulelink,
'moduleversion' => $version,
'currentPage' => $page,
'tplVars' => (new TemplateService)->$CustomFunction($vars),
'addonlang' => $_lang,
),
);
}
And the TemplateService handles the additional variables if needed. In this case we define the function related to the template
class TemplateService {
public function template_home($vars) { // home is the name of the template file that will be displayed
return []; // Return a Value or a Array with additional variables that need to be available inside the template.
}
}