2014-02-12 115 views
0

我想跟着mvc tutorial並安裝code與擴展管理器。Joomla的主模板

這一切工作正常和丹迪,但我想弄清楚所有頁面的其餘部分來自哪裏。該模板只打印出「Hello World」,但該頁面與菜單和全部一起完成。

有沒有辦法只打印出「Hello World」? following表明我可以編輯某些文件(未指定內容)並將其打印出JSON,但當輸出被某種主頁面模板包圍時,它將是無效的JSON。

安裝插件後,我有以下文件:

/components/com_helloworld/helloworld.php

<?php 

// No direct access to this file 
defined('_JEXEC') or die('Restricted access'); 

// import joomla controller library 
jimport('joomla.application.component.controller'); 

// Get an instance of the controller prefixed by HelloWorld 
$controller = JController::getInstance('HelloWorld'); 

// Perform the Request task 
$controller->execute(JRequest::getCmd('task')); 

// Redirect if set by the controller 
$controller->redirect(); 

/components/com_helloworld/controller.php

<?php 
// No direct access to this file 
defined('_JEXEC') or die('Restricted access'); 

// import Joomla controller library 
jimport('joomla.application.component.controller'); 

/** 
* Hello World Component Controller 
*/ 
class HelloWorldController extends JController 
{ 
} 

/組件/com_helloworld/views/helloworld/view.html.php

<?php 
// No direct access to this file 
defined('_JEXEC') or die('Restricted access'); 

// import Joomla view library 
jimport('joomla.application.component.view'); 

/** 
* HTML View class for the HelloWorld Component 
*/ 
class HelloWorldViewHelloWorld extends JView 
{ 
    // Overwriting JView display method 
    function display($tpl = null) 
    { 
     // Assign data to the view 
     $this->msg = 'Hello World'; 

     // Display the view 
     parent::display($tpl); 
    } 
} 

/components/com_helloworld/views/helloworld/tmpl/default.php

<?php 
// No direct access to this file 
defined('_JEXEC') or die('Restricted access'); 
?> 
<h1><?php echo $this->msg; ?></h1> 

我可以輸出東西/components/com_helloworld/helloworld.php和離開它,但沿的方式思維更生成輸出和控制器獲取數據的視圖。

回答

0

添加&format=json更改jDocument類型(我猜)並停止將模板封裝在「主」模板中。擁有/components/com_helloworld/views/helloworld/view.json.php將生成所需的頭文件。

這工作,因爲我有以下文件:/public_html/libraries/joomla/document/json/json.php

包含以下內容:

<?php 
defined('JPATH_PLATFORM') or die; 
class JDocumentJSON extends JDocument 
{ 
    protected $_name = 'joomla'; 
    public function __construct($options = array()) 
    { 
     parent::__construct($options); 

     // Set mime type 
     $this->_mime = 'application/json'; 

     // Set document type 
     $this->_type = 'json'; 
    } 
    public function render($cache = false, $params = array()) 
    { 
     JResponse::allowCache(false); 
     JResponse::setHeader('Content-disposition', 'attachment; filename="' . $this->getName() . '.json"', true); 

     parent::render(); 

     return $this->getBuffer(); 
    } 
    public function getName() 
    { 
     return $this->_name; 
    } 
    public function setName($name = 'joomla') 
    { 
     $this->_name = $name; 

     return $this; 
    } 
} 

我的猜測是,你基本上可以任意添加文件那裏並輸出任何類型的響應,雖然基本知識似乎已經存在(也許我會添加jsonp)。