a must have Joomla code snippet

    User Rating: 1 / 5

    Star ActiveStar InactiveStar InactiveStar InactiveStar Inactive
     

    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:

        $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');  

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

    Tags:

    Please publish modules in offcanvas position.