Today we are sharing two new functions.

We needed to get the current and the default Joomla templates. Current template is the template style assigned to the menu entry. Default template is template used when no template style is assigned to menus.

A requisite was that both methods must work on plugins so the alternative method based on menu object ( commonly seen on internet ) wasn’t valid for us.

/**
* Get the name of the default themplate
* @author Jiden - Digital Disseny, S.L.
* @version 18/04/2012
*
* @param 0/1 $client_id - 0 frontend default | 1 backend default
*/
public function getDefaultTplName($client_id = 0) {
$result = null;
$db = JFactory::getDBO();
$query =  " SELECT template FROM #__template_styles "
." WHERE client_id=".(int)$client_id." "
." AND home = 1 ";
$db->setQuery($query);
try {
$result = $db->loadResult();
} catch (JDatabaseException $e) {
return $e;
}

return $result;
/**
* Get the name of the current template
* Works also @ plugins method
* @author Jiden - Digital Disseny, S.L.
* @version 23/04/2012
*
* @return string the name of the active template
*/
public function getCurrentTplName() {
// required objects
$app =& JFactory::getApplication();
$jinput = $app->input;
$db = JFactory::getDBO();
// default values
$menuParams = new JRegistry();
$client_id = $app->isSite() ? 0 : 1;
$itemId = $jinput->get('Itemid',0);
$tplName = null;

// try to load custom template if assigned
if ($itemId) {
$sql = " SELECT ts.template " .
" FROM #__menu as m " .
" INNER JOIN #__template_styles as ts" .
" ON ts.id = m.template_style_id " .
" WHERE m.id=".(int)$itemId." " .
"";
$db->setQuery($sql);
$tplName = $db->loadResult();
}
// if no itemId or no custom template assigned load default template
if( !$itemId || empty($tplName)) {
$tplName = getDefaultTplName($client_id);
}

return $tplName;
}
}