a must have Joomla code snippet
As a programmer I don’t have a good memories that’s why I have to read this Joomla code snippet when creating or updating joomla templates
get current (absolute) url:
<pre class="brush:php">$uri = &JURI::getInstance();
$myabsoluteurl = $uri->toString(array(‘path’));
get article by article id:
<pre class="brush:php">$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 “;
add js file to head of the template:
<pre class="brush:php">$host = JURI::root();
$document =& JFactory::getDocument(); $document->addScript($host.‘path/jquery-1.4.4.min.js’);
render module from template:
<pre class="brush:php">jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule(‘mod_related_items’); $attribs[‘style’] = ‘xhtml’; echo JModuleHelper::renderModule( $module, $attribs );
get active user id:
<pre class="brush:php">$user =& JFactory::getUser();
$user_id = $user->id;
fix global $mainframe variable:
<pre class="brush:php">//old code
global $mainframe; $params = &$mainframe->getParams(); //in joomla 1.7/2.5 $app = &JFactory::getApplication(); $params = $app->getParams();
get one record from db:
<pre class="brush:php">$db = &JFactory::getDBO();
$query = “SELECT * FROM uk_code WHERE code = ‘”.$_to_postcode.”’ LIMIT 1”; $db->setQuery($query); if ($row = $db->loadObject ()) { $x2 = $row->coord1; }
set page title:
<pre class="brush:php">$document = JFactory::getDocument();
$document->setTitle($this->escape($title));
detect whether in front page:
<pre class="brush:php">$uri = &JURI::getInstance();
$myabsoluteurl = $uri->toString(array(‘path’)); if($myabsoluteurl==JURI::base(true).’/’) $front_page = TRUE;
get component name from joomla template index.php
<pre class="brush:php">$comp_name = JFactory::getApplication()->input->get('option'); </pre>
get view name from joomla template index.php
<pre class="brush:php">$view_name = JFactory::getApplication()->input->get('view'); </pre>
get article id from joomla template index.php
<pre class="brush:php">$article_id = JFactory::getApplication()->input->get('id'); </pre>
If you have some nice code snippets please share with us and I will update this list, later.