我想将一个页面(页面使用自己的模板)添加到另一个页面调用的视图中。
这是有效的,我得到的数据然而我得到3copies的blog.php出现在index.php我不明白为什么这样做。
的index.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>
blog.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>
答案 0 :(得分:2)
好的,你在这里缺少一些基础知识。
页/ 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在它的模板中查找,重复它足够的次数并将结果放入。内部的任何标签都将自动分配给具有确切名称的模型字段。
为您节省大量的打字:)