2013-02-13 68 views
1

我想了解如何在joomla 2.5中开发自定义组件,并且在第一步中卡住,我想知道什么是使用assignRef()函数和for更多信息click here什么是joomla 2.5中的assignRef()函数2.5

<?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); 
    } 
} 

在assignRef()函数,第一个参数作为变量不是一个值,因为如果我改变它的价值,那么一些其他的事情是不能够显示$问候的值: -

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

// 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); 
    } 
} 

然后在网站/视图/你好/ TMPL /如default.php,如果我写这样的,然后它显示我正确的答案: -

<?php 

// No direct access 

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

那么结果将是:----你好世界

我知道,对你而言,这是一个简单或天真的问题,但对我来说,这是我自己发展领域的新时代的开始..任何事情都会受到最大的赞赏。 。

回答

1

assignRef()函数为视图添加一个变量。所以它可以通过视图类访问。来源:here

但是,我可以建议您遵循Joomla 2.5 here的扩展创建教程,而不是您正在使用的1.5教程,以便您不使用弃用的函数。例如在Joomla 2.5中,assignRef()不再需要。该模型从表中检索数据和所需要的所有仅仅是

$this->items = $items; 
2

在的Joomla 1.5,有两个功能assign()assignRef()被用于从一个视图从的Joomla 1.6和传递数据到layout.But在它刚刚通过添加数据到视图对象完成。自从Joomla 1.6/2.5至少需要PHP 5.2,它具有更好的内存管理,这是引入这两种方法的主要原因。这两个 方法是通过引用而不是按值分配变量。 PHP4 默认情况下使用按值赋值,而PHP5(使用对象时)使用 通过引用进行赋值。

如果您在使用Joomla最新版本,你可以通过把

$this->variable = $something;

view.html.php做到这一点,它会在布局中可用。