0

I use a template system which is little bit similar to MVC. It uses .tpl files for outputing html. And I am trying to use a comment class with it. for example you create comments.tpl

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

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

</THEME>

And to echo this you would do this

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

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

In the comment class it uses simple echo command but my template system does not like echo. it does not show properly unless you use $_PAGE = ""; string

This is the comment class. I tried to change echo with return but it does not show anything. I tried to add return to other private functions it shows only the comment not the parents for example.

Could you guys please give an idea how do I approch this problem or what is the solutions? thanks.

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


            }
?>
intuitivepixel
  • 23,302
  • 3
  • 57
  • 51
Erdem Ece
  • 1,755
  • 5
  • 22
  • 42
  • 1
    Don't echo in your comments class, return a value from the comments class that can be echoed by the template engine – Mark Baker Sep 07 '13 at 11:32
  • when i replace echo with return it does not show anything. I tried that. – Erdem Ece Sep 07 '13 at 11:33
  • @ErdemEce Even know your returning, you still need to echo --- http://pastebin.com/HFgfepJV – Daryl Gill Sep 07 '13 at 11:36
  • @DarylGill yes I know. $_PAGE does the echoing anyway. I use other classes with return and they work. but this. something wrong with the fucntioning in the class. – Erdem Ece Sep 07 '13 at 11:38
  • **Your class should not render/echo anything!!!** This should do binding not sending. Also there are another flaws : 1) A constructor should never do any computations. It's meant to initialize the state of the class (for example, inject required dependencies). 2) You're breaking encapsulation by making properties public - `public $contentid;` - They should not be public, but either private or protected - you'd have to defined setters and getters for them. – Yang Sep 07 '13 at 13:49
  • @DaveJust thanks for your comment. could yo give me some example or help me to correct the class? thanks. – Erdem Ece Sep 07 '13 at 19:19
  • @ErdemEce, two notes .. templates have nothing to do with MVC and you really need to look into [this article](http://codeangel.org/articles/simple-php-template-engine.html). – tereško Sep 08 '13 at 12:34
  • @tereško i did not say that but thanks for the suggestion. I really need to find a solutuion to this. – Erdem Ece Sep 09 '13 at 15:01

0 Answers0