Joomla: get category ID in template

I just needed to specify special layout for the articles of selected categories. I was looking for some solution online, but none of them worked. Mostly you can find this:

JRequest::getInt('catid');

or

JRequest::getInt('catid', '', 'get');

Maybe they worked in some other version of Joomla, but only working code I found is this (little modified by me to return integer values):


function getCategory() {
$iId = JRequest::getVar('id',0);
$database = &JFactory::getDBO();
if(JRequest::getVar('view', 0) == "section"){
return (int) JRequest::getVar( 'id', 0);
}else if(Jrequest::getVar( 'view', 0) == "category"){
$sql = "SELECT id FROM #__categories WHERE id = '$iId'";
$database->setQuery( $sql );
$row=$database->loadResult();
return (int) $row;
}else if(Jrequest::getVar('view', 0) == "article"){
$temp = explode(":",JRequest::getVar('id',0));
$sql = "SELECT catid FROM #__content WHERE id = ".$temp[0];
$database->setQuery( $sql );
$row=$database->loadResult();
return (int) $row;
}
}
$categoryId = getCategory();