教程栏目

joomla中文网出品的官方教程

上一节中我们介绍了控制器类中的display方法,在本节中我们将介绍一下视图类的display方法。虽然二者的名称一样,但是执行逻辑完全不一样。

 

一段典型的视图类的display方法的代码如下:

function display($tpl = null)
{
$items = $this->get('Items');
$pagination = $this->get('Pagination');
$state = $this->get('State');
if(count($error = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />',$errors));
return false;
}

$this->items = $items;
$this->pagination = $pagination;

$this->state = $state;
$this->addToolBar();

if($this->getLayout() !== 'modal')
{
downloadHelper::addSubmenu('items');
}
$this->sidebar = JHtmlSidebar::render();
parent::display($tpl);

}

在 视图中开始创建我们的页面了,首先的几行代码就是从模型类中获得数据。在上面的代码中,getStates()方法继承自 JModel,getItems()方法和getPagnination()方法都是继承自JModelList.getState方法的作用是返回状态 对象。这个对象代表着当前的屏幕,包括过滤器,分页,排序等等状态。

当我们从模型获得到了想要的数据,开始检查是否出现错误。如果没有错误,那么我们就使用addToolbar()方法。这个方法添加工具栏的图标到页面。实际上工具栏是由系统后的mod_toolbar模块产生的。这个模块的工作原理就是显示全局工具栏对象。

最后我们调用父类(JViewLegacy)类的display方法,这个方法代码如下:

public function display($tpl = null)
{
$result = $this->loadTemplate($tpl);

if ($result instanceof Exception)
{
return $result;
}

echo $result;
}

在 这个方法中吗,执行loadTemplate()方法,并且将结果保存到$result变量中,由于我们已经启用了ob_start(),所有echo并 不会直接输出到屏幕而是输出到缓冲区中,在loadTemplate方法中检查是否存在模板输出覆盖 ,并且加载语言文件。

作者: 樱木花道

Joomla程序员,从J1.5到J4.x始终都在做Joomla相关开发定制工作,有超过10年行业经验,国内Joomla扩展开发商ZMAX团队的核心成员

作者网站:ZMAX程序人

评论 (0)

  • 最新在前
  • 最佳在前

第1章 Joomla入门教程

第3章 C计划

第5章 E计划

第6章 H计划

第7章 G计划

第9章 运行环境

第11章 主从与集群

第12章 模块开发

第13章 插件开发

第14章 j2.x组件开发教程

第15章 页面定制教程

第16章 页面构造器

第17章 joomla升级