2013-04-15 99 views
6

如何根據本示例如何從控制器傳遞變量到視圖的Joomla MVC

class MYControllerControllerParser extends JController{ 

      public function __construct($default = array()) { 

      parent::__construct($default); 

     } 

    protected function _import($file, $type) { 

      $layout = ''; 
      switch ($type) { 

       case 'importcsv': 
        $contains_headers  = false; 
        $field_separator = JRequest::getVar('separator'); 
        $field_separator = empty($field_separator) ? ',' : $field_separator; 
        $field_enclosure = JRequest::getVar('enclosure');; 
        $field_enclosure = empty($field_enclosure) ? '"' : $field_enclosure; 
//this variable should be passed to the view 
        $this->info = $this->getImportInfoCSV($file, contains_headers, $field_separator, $field_enclosure); 
//This variable should go to view 
        $this->file = basename($file); 
        $layout = 'importcsv'; 
        break; 
      } 

    $this->getView('import','html')->display(); 
    } 
    } 

回答

10

在控制器通過我的變量從的Joomla子控制器的視圖:

$view = $this->getView('import','html'); 
$view->myVariable = 'hello'; 
$view->display(); 

在查看:

class MycomponentViewItem extends JViewLegacy 
{ 
    /** @var string my variable */ 
    public $myVariable; 

    public function display($tpl = null) 
    { 
    $myVariable = $this->myVariable; 
    //... 
    } 
} 
+0

非常感謝工作! – fefe

+0

@pirtr_cz如何在佈局中使用$ myVariable? –

+1

@Harsimran>嘗試'$ this-> myVariable'因爲佈局是視圖的一部分 –

相關問題