Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display view without template?

I have view (frontend) in my own component (view.html.php):

class MevViewMev extends JView{
        function display($tpl = null){
                parent::display($tpl);
        }
}

And template:

<?php defined('_JEXEC') or die('Restricted access'); ?>
<div>
ASFADSFDSF
</div>

How to display it without joomla template (head section, styles, etc). I want to call this part of jquery onclick method in the window.

like image 688
Nips Avatar asked Dec 17 '11 15:12

Nips


3 Answers

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.

like image 117
Babur Ussenakunov Avatar answered Oct 30 '22 06:10

Babur Ussenakunov


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
    }
}
like image 34
ekerner Avatar answered Oct 30 '22 06:10

ekerner


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();
}
like image 1
mavrosxristoforos Avatar answered Oct 30 '22 06:10

mavrosxristoforos