Jasa Pembuatan Aplikasi Murah Berkualitas
konsultasi gratis sekarang

a must have Joomla code snippet

Published on Saturday, Nov 2, 2013

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

  1. get current (absolute) url:

<pre class="brush:php">$uri = &JURI::getInstance();

$myabsoluteurl = $uri->toString(array(‘path’));

  1. 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 “;

  1. 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’);

  1. 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 );

  1. get active user id:

<pre class="brush:php">$user =& JFactory::getUser();

$user_id = $user->id;

  1. 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();

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

  1. set page title:

<pre class="brush:php">$document = JFactory::getDocument();


  1. 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;

  1. get component name from joomla template index.php 

<pre class="brush:php">$comp_name  = JFactory::getApplication()-&gt;input-&gt;get('option');    </pre>
  1. get view name from joomla template index.php 

<pre class="brush:php">$view_name  = JFactory::getApplication()-&gt;input-&gt;get('view');  </pre>
  1. get article id from joomla template index.php 

<pre class="brush:php">$article_id  = JFactory::getApplication()-&gt;input-&gt;get('id');  </pre>

If you have some nice code snippets please share with us and I will update this list, later.