Posts tagged templates

Joomla: functions to get current/default template

0

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;
}
}

Joomla: function to get template positions

0

We needed a function to get available template positions. Didn’t found lot of info out there so we created our own function.

/**
* Parse templateDetails.xml and get available positions
* @author Jiden - Digital Disseny, S.L.
* @version 04/04/2012
*
* @param string $tplPath - path of the current theme
* @param boolean $nameAsKey - set position name as output array key?
*
* @return array of positions
*/
function getTplPositions($tplPath, $nameAsKey = false) {
$outputArray = array();
$templateXML = $tplPath.'/templateDetails.xml';
// try to load positions from template XML file
$xml = JFactory::getXML($templateXML);
if (is_object($xml)) {
$positions = $xml->xpath('positions/position');
if ($positions) {
foreach ($positions as $position) {
$name = (string)$position->data();
// clean name
$name = preg_replace("/(\-a|\-b|\-c|\-d|\-e|\-f)$/i", "", $name);
// do not add duplicates
if(!in_array($name, $outputArray)) {
// use position name as array key?
if($nameAsKey) $outputArray[$name] = $name;
else $outputArray[] = $name;
}
}
}
}
return $outputArray;
}

Now you can use the function as:

$tplPath = JPATH_SITE . '/templates/' . $this->template;
$positions = getTplPositions();
print_r($positions);

That will return  something like:

Array
(
    [0] => debug
    [1] => header-top
    [2] => header
    [3] => header-bottom
    [4] => topmenu
    [5] => rsidebar-top
    [6] => rsidebar
    [7] => rsidebar-bottom
    [8] => lsidebar-top
    [9] => lsidebar
    [10] => lsidebar-bottom
    [11] => main-top
    [12] => main
    [13] => main-bottom
    [14] => component-top
    [15] => component
    [16] => component-bottom
    [17] => footer-top
    [18] => footer
    [19] => footer-bottom
Go to Top