1
votes

i am trying to understand about how to develop custom components in joomla 2.5 and on the very first step i get stuck and i want to know what is the use assignRef() function and for more info click here

<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
*/

// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */

class HelloViewHello extends JView
{
    function display($tpl = null)
    {
        $greeting = "Hello World!";
        $this->assignRef( 'greeting', $greeting );

        parent::display($tpl);
    }
}

In assignRef() function, the first parameter acts as variable not a value because if i am changing it's value to some other thing then it is not able to showing the value of $greeting:-

http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1 * @license GNU/GPL */

// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */

class HelloViewHello extends JView
{
    function display($tpl = null)
    {
        $greeting = "Hello World!";
        $this->assignRef( 'greeting123', $greeting );

        parent::display($tpl);
    }
}

then in site/views/hello/tmpl/default.php, if i write like these then it is showing me the correct answer:-

<?php

// No direct access

defined('_JEXEC') or die('Restricted access'); ?>
<h1><?php echo $this->greeting123; ?></h1>

Then the result will be:---- Hello world

I know that for you it's been a simple or a naive question but for me it's the beginning of a new era in my own development field..Anything will be appreciated the most..

2

2 Answers

3
votes

In Joomla 1.5 ,There are two function assign() and assignRef() are used to pass data from a view into the layout.But from Joomla 1.6 and above it just done by adding the data to the view object.since Joomla 1.6/2.5 requires at least PHP 5.2, which has far better memory management, which is the main reason why those two methods were introduced. Those two methods are assigning the variables by reference and not by value. PHP4 by default used assign by value, while PHP5 (when using objects) uses assign by reference.

IF you are using Joomla latest version you can just do it by putting

$this->variable = $something;

in your view.html.php and it will be available in the layout.

1
votes

The assignRef() function adds a variable to the view. So its then accessible across the view class. Source: here

May I suggest however that you follow the extension creation tutorial for Joomla 2.5 here rather than the 1.5 tutorial you are using so that you don't use deprecated functions. For example in Joomla 2.5 assignRef() is no longer needed. The model retrieves the data from the table and all that is needed is simply

$this->items = $items;