Guides & Tutorials

Technology tutorials, technology guides and more

As a programmer you don't have to have a good memory, that's why you have to read this Joomla code snippet when creating or updating a Joomla template.

1. Get current (absolute) url:

$uri =& JURI::getInstance();
$myabsoluteurl = $uri->toString(array('path'));

2. Get article by article id:

$db =& JFactory::getDBO();
$sql = "SELECT introtext FROM #__content WHERE id = ".intval($articleId);
$db->setQuery($sql);
$fullArticle = $db->loadResult();
if (!strlen(trim($fullArticle)))
{
	$fullArticle = "Article is empty ";
}

3. Add js file to head of the template:

$host = JURI::root();
$document =& JFactory::getDocument();
$document->addScript($host.'path/jquery-1.4.4.min.js');

4. Render module from template:

jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('mod_related_items');
$attribs['style'] = 'xhtml';
echo JModuleHelper::renderModule( $module, $attribs );

5. Get active user id:

$user =& JFactory::getUser();
$user_id = $user->id;

6. Fix global $mainframe variable:

// old code
global $mainframe;
$params =& $mainframe->getParams();
// in joomla 1.7/2.5
$app =& JFactory::getApplication();
$params = $app->getParams();

7. Get one record from db:

$db =& JFactory::getDBO();
$query = "SELECT * FROM uk_code WHERE code = '".$_to_postcode."' LIMIT 1";
$db->setQuery($query);
if ($row = $db->loadObject ())
{
	$x2 = $row->coord1;
}

8. Set page title:

$document = JFactory::getDocument();
$document->setTitle($this->escape($title));

9. Detect whether in front page:

$uri =& JURI::getInstance();
$myabsoluteurl = $uri->toString(array('path'));   
if($myabsoluteurl==JURI::base(true).'/')
{
	$front_page = TRUE;
}

10. Get component name from joomla template index.php

$comp_name = JFactory::getApplication()->input->get('option');

11. Get view name from joomla template index.php:

$view_name = JFactory::getApplication()->input->get('view');

12. Get article id from joomla template index.php:

$article_id = JFactory::getApplication()->input->get('id');