How to display view without template?

纵然是瞬间 提交于 2019-12-04 02:51:21

To display the component only add "tmpl=component" parameter to url. If need to display something besides component's view it can be customized - create "component.php" file in template's root folder and include in it whatever you need. More templates can be done in the same way - create "some_template.php" in template's root folder and add "tmpl=some_template" parameter to url.

ekerner

Start Edit

OK so the below works, but I found a better way. In your controller do ...

if (JRequest::getVar('format') != 'raw') {
    $url = JURI::current() . '?' . $_SERVER['QUERY_STRING'] . '&format=raw';
    header('Location: ' . $url);
    // or, if you want Content-type of text/html just use ...
    // redirect($url);
}

End Edit

You can set 'tmpl' to 'component', as suggested by Babur Usenakunov, in which case scripts and css may be loaded, like ...

JRequest::setVar('tmpl','component');

However if you want to create raw output you can add &format=raw or in your component make a view of type 'raw' ...

Unfortunately the only functional way I can find to make a viewType of raw render correctly is to call exit() after the view class calls parent::display() ...

In your controller.php ...

class com_whateverController() extends JController
{
    function __construct()
    {
        // the following is not required if you call exit() in your view class (see below) ...
        JRequest::setVar('format','raw');
        JFactory::$document = null;
        JFactory::getDocument();
        // or
        //JFactory::$document = JDocument::getInstance('raw');
        parent::__construct();
    }

    function display()
    {
        $view = $this->getView('whatever', 'raw');
        $view->display();
    }

}

then in views/whatever/view.raw.php ...

class com_whateverViewWhatever extends JView
{
    public function display($tpl = null)
    {
            parent::display();
            exit; // <- if you dont have this then the output is captured in and output buffer and then lost in the rendering
    }
}

I know this comes in very late, but for future readers, here's how I did it for my extension, without editing the template, or adding anything in the URL (since I have control over neither of those):

jimport('joomla.application.component.view');
use \Joomla\CMS\Factory;

// Comp stands for the Component's name and NoTmpl stands for the View's name.
class CompViewNoTmpl extends \Joomla\CMS\MVC\View\HtmlView {

// Force this view to be component-only
public function __construct() {
  $app = Factory::getApplication();
  $app->input->set('tmpl', 'component');
  parent::__construct();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!