Joomla 如何显示没有模板的视图?

Joomla 如何显示没有模板的视图?,joomla,joomla-template,Joomla,Joomla Template,我在自己的组件(view.html.php)中有视图(前端): 和模板: <?php defined('_JEXEC') or die('Restricted access'); ?> <div> ASFADSFDSF </div> ASFADSFDSF 如何在没有joomla模板的情况下显示它(标题部分、样式等)。我想在窗口中调用jquery onclick方法的这一部分。要仅显示组件,请将“tmpl=component”参数添加到url。 如果需要显

我在自己的组件(view.html.php)中有视图(前端):

和模板:

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

ASFADSFDSF

如何在没有joomla模板的情况下显示它(标题部分、样式等)。我想在窗口中调用jquery onclick方法的这一部分。

要仅显示组件,请将“tmpl=component”参数添加到url。 如果需要显示组件视图以外的内容,可以自定义它-在模板的根文件夹中创建“component.php”文件,并在其中包含任何您需要的内容。 更多的模板可以用同样的方法完成-在模板的根文件夹中创建“some_template.php”,并将“tmpl=some_template”参数添加到url。

开始编辑

好的,下面的方法很有效,但我找到了一个更好的方法。在你的控制器中做

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);
}
结束编辑

您可以按照Babur Usenakunov的建议将“tmpl”设置为“component”,在这种情况下,可以加载脚本和css,如

JRequest::setVar('tmpl','component');
但是,如果您想创建原始输出,可以添加&format=raw或在组件中创建“raw”类型的视图

不幸的是,我能找到的正确生成原始渲染的viewType的唯一功能方法是在view类调用parent::display()之后调用exit()

在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();
    }

}
然后在views/where/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
    }
}
class com\u whateview扩展JView的内容
{
公共功能显示($tpl=null)
{
父::显示();

退出;//我知道这很晚才会出现,但对于未来的读者,我是如何在不编辑模板或在URL中添加任何内容的情况下为我的扩展完成这项工作的(因为我无法控制这两项):

真不敢相信“在模板的根文件夹中创建”component.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
    }
}
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();
}