1

我想将一个页面(页面使用自己的模板)添加到另一个页面调用的视图中。

这行得通,我得到了数据,但是我得到了 index.php 上显示的 blog.php 的 3 个副本,我不明白为什么要这样做。

索引.php:

<?php
class page_index extends Page {
    function init(){
        parent::init();
        $p=$this;

        $p=$this->add('View',null,null,array('view/home'));
        $p->template->tryset('pageblog',$this->add('page_blog'));

    }
}

home.html(由 index.php 调用):

<div>
<?$pageblog?>
</div>

博客.php:

<?php
class page_blog extends Page {
    function init(){
        parent::init();
        $page=$this;

        //Get Articles
        $articles=$this->add('Model_News')->getRows();

        $page->add('H1')->set('Latest News');

        foreach($articles as $article){
            $content=$this->add('view',null,null,array('view/blog'));
            $content->template->set('title',$article['title']);
            $content->template->set('content',$article['content']);
        }

    }
}

blog.html(blog.php 的模板)

<div>
<h3><?$title?></h3>
<p><?$content?></p>
<hr>
</div>
4

1 回答 1

2

好的,您在这里缺少一些基础知识。

  1. 您不添加页面。ApiFrontend 为您完成。
  2. 您可以为现有页面定义模板,而无需使用 defaultTemplate() 添加类似的视图
  3. 将对象添加到对象中时,可以将其放置到点中。如果您手动将对象插入到模板中,这不是一件好事。
  4. 您可以使用 lister 来显示这样的条目。

页面/index.php

class page_index extends Page {
    function init(){
        parent::init();
        $this->add('MVCLister',null,'News','News')->setModel('News');

    }
    function defaultTemplate(){
        return array('page/home');  // separate pages from views to avoid mess in templates
    }
}

模板/默认/页面/home.html:

<div>
<h1>My Blog page</h1>
<p>Welcome to my blog</p>
<hr/>
 <?News?>
   <?rows?>
   <?row?>
   <div><h3><?$title?></h3>
   <?$content?>
   </div>
   <?/row?>
   <?/rows?>
 <?/News?>
</div>

现在我需要对 MVCLister 发表评论。和MVCGrid类似,但是默认没有模板,所以需要指定。第三个参数定义您希望新闻在页面上显示的位置。第四个参数是模板,通常指定为“array(...)”。没有数组 - 它从它的父模板中取出一个块。因此,在这种情况下,我们将内容用于 MVCLister,并将内容放回相同的标签中,替换您现在拥有的内容。

MVCLister 在它的模板中查找,重复足够多次并将结果放入 . 内部的任何标签都将自动分配给具有确切名称的模型字段。

为您节省大量打字时间:)

于 2011-09-08T11:11:01.313 回答