Php 从类回显到模板系统
我使用的模板系统有点类似于MVC。它使用.tpl文件输出html。我正在尝试使用一个注释类。例如,创建comments.tplPhp 从类回显到模板系统,php,oop,template-engine,Php,Oop,Template Engine,我使用的模板系统有点类似于MVC。它使用.tpl文件输出html。我正在尝试使用一个注释类。例如,创建comments.tpl <THEME Name={Comments} Var={Comment}> <div class="well"> <VAR>Comment</VAR> </div> </THEME> 不要在comments类中回显,从comments类返回一个值,该值可以由模板
<THEME Name={Comments} Var={Comment}>
<div class="well">
<VAR>Comment</VAR>
</div>
</THEME>
不要在comments类中回显,从comments类返回一个值,该值可以由模板引擎回显当我用return替换回显时,它不会显示任何内容。我试过了。@ErdemEce即使知道你回来了,你仍然需要回应---@DarylGill是的,我知道$_不管怎样,佩奇都会发出回声。我使用带有return的其他类,它们可以工作。但是这个。类中的函数有问题。您的类不应该呈现/回显任何内容!!!这不应该是绑定,而不是发送。此外还有另一个缺陷:1)构造函数不应该进行任何计算。它用于初始化类的状态(例如,注入所需的依赖项)。2) 通过公开属性-
public$contentid,您正在破坏封装代码>-它们不应该是公共的,而应该是私有的或受保护的-您必须为它们定义setter和getter。
<?php
// Comments class
$Comment = new GetComments($dbc, $data['ContentID']);
$Comments = t_Comments($Comment->print_comments());
// Echo
$_PAGE = t_Comments($Comments);
?>
<?php
class GetComments {
public $contentid;
public $results = array();
public $parents = array();
public $children = array();
protected $db;
public function __construct(PDO $db, $contentid)
{
$sql = $db->prepare(" SELECT * FROM comments WHERE ContentID = :ContentID ");
$sql->execute(array(":ContentID" => $contentid));
while($ROW = $sql->fetch(PDO::FETCH_ASSOC)){
$this->results[] = $ROW;
}
foreach ($this->results as $comment)
{
if ($comment['Parent'] < 1)
{
$this->parents[$comment['CommentsID']][] = $comment;
}
else
{
$this->children[$comment['Parent']][] = $comment;
}
}
}
/**
* @param array $comment
* @param int $depth
*/
private function format_comment($comment, $depth)
{
for ($depth; $depth > 0; $depth--)
{
echo "--";
}
echo $comment['Comment'];
echo "<br />";
}
/**
* @param array $comment
* @param int $depth
*/
private function print_parent($comment, $depth = 0)
{
foreach ($comment as $c)
{
$this->format_comment($c, $depth);
if (isset($this->children[$c['CommentsID']]))
{
$this->print_parent($this->children[$c['CommentsID']], $depth + 1);
}
}
}
public function print_comments()
{
foreach ($this->parents as $c)
{
$this->print_parent($c);
}
}
}
?>