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

时间:2011-12-17 15:50:18

标签: joomla joomla-template

我在自己的组件中查看(前端)(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 个答案:

答案 0 :(得分:21)

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

答案 1 :(得分: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');

但是,如果要创建原始输出,可以添加&amp; format = raw或在组件中创建“原始”类型的视图......

不幸的是,我能找到正确渲染viewType的唯一函数方法是在视图类调用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
    }
}

答案 2 :(得分:0)

我知道这很晚了,但是对于未来的读者来说,这是我为扩展程序执行的操作,而无需编辑模板或在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();
}
相关问题