从类到模板系统的回应

时间:2013-09-07 11:29:18

标签: php oop template-engine

我使用的模板系统与MVC有点类似。它使用.tpl文件输出html。我正在尝试使用它的评论类。例如,您创建comments.tpl

<THEME Name={Comments} Var={Comment}>

    <div class="well">
        <VAR>Comment</VAR>
    </div>

</THEME>

为了回应这个,你会这样做

<?php
    // Comments class
    $Comment = new GetComments($dbc, $data['ContentID']);
    $Comments = t_Comments($Comment->print_comments());

    // Echo
    $_PAGE = t_Comments($Comments);
?>

在注释类中,它使用简单的echo命令,但我的模板系统不喜欢echo。除非您使用$_PAGE = "";字符串

,否则它无法正确显示

这是评论课。我试图用return返回echo,但它没有显示任何内容。我试图将返回添加到其他私有函数,它只显示注释而不是父母。

请问您如何解决这个问题或者解决方案是什么?感谢。

comment_class.php

<?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);  
                    }  
                }


            }
?>

0 个答案:

没有答案