如何在没有模板的情况下显示视图?

10

我有一个视图(前端)在我的自定义组件中(view.html.php):

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

并且模板:

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

如何在不使用Joomla模板(头部、样式等)的情况下显示它。我想在窗口中通过jQuery的onclick方法调用这部分内容。

3个回答

21

只需在网址中添加“tmpl=component”参数即可显示组件。
如果需要显示组件视图以外的内容,则可以自定义 - 在模板的根文件夹中创建“component.php”文件,并在其中包含所需内容。
可以使用相同的方式制作更多模板 - 在模板的根文件夹中创建“some_template.php”文件,并在网址中添加“tmpl=some_template”参数。


无法相信“在模板的根文件夹中创建'component.php'文件”这个想法从未发生过我。 - Ejaz

4

开始编辑

好的,下面的方法可以实现,但是我发现有更好的方法。在您的控制器中执行以下操作...

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”的视图...

不幸的是,我找到的唯一使“raw”视图类型正确渲染的方法是在视图类调用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/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
    }
}

3

我知道这个回答有点晚了,但是为了后来的读者,我来分享一下我是如何在我的扩展中实现这个功能的,而不需要编辑模板或添加任何URL(因为我对这两个都没有控制权):

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接