【问题标题】:How to display view without template?如何在没有模板的情况下显示视图?
【发布时间】:2012-01-22 16:05:04
【问题描述】:

我在自己的组件(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方法。

【问题讨论】:

    标签: joomla joomla-template


    【解决方案1】:

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

    【讨论】:

    • 不敢相信“在模板的根文件夹中创建“component.php”文件”我从未想过。
    【解决方案2】:

    开始编辑

    好的,所以下面的工作,但我找到了一个更好的方法。在你的控制器中做...

    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' 类型的视图 ...

    不幸的是,我能找到正确制作原始渲染视图类型的唯一功能方法是在视图类调用 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();
      }
      

      【讨论】:

        猜你喜欢
        • 2016-04-24
        • 2018-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-05
        • 2015-05-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多