开发者

PHP Custom templating class [closed]

开发者 https://www.devze.com 2023-04-10 23:54 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

Since I decided to reinvert the wheel and write a growing MVC, I though having a template class will be cool. Now I have never done any complex templating apart from simple include kind of templating. Now I wrote this base class and here is my concept is explained below. Is this right way/concept of writing web page templates?

Concept "Any Template class will extend this class. The Controller will modify single part or all. If controller will modify the content only using set action, then the page will change only one component and other parts will remain the same just like including header/footer would do"

Another question is,how do I pass same page object so that non changed parts are cached in the object (since http is stateless protocol)? Should I start session and store it object in session? Should parent be a singleton? Thanks!

<?php

class TemplateBase{
    //variables corresponding to part of web page
    private $header;
    private $footer;
    private $body;
    private $menu;
    private $left_side;
    private $right_side;
    private $menu_header;//between menu and header
    private $menu_content;//between menu and content

    public function __construct(){
        //no inits for now
    }

    //header
    public function header_func($action, $value=null){
        if($action=="get"){
            return $this->header;
        }

        if($action=="set"){
            $this->header = $value;
        }
    }

    //footer
     public function footer_func($action, $value=null){
         if($action=="get"){
            return $this->footer;
        }

        if($action=="set"){
            $this->footer = $value;
        }
    }

    //body
     public function contents_func($action, $value=null){
         if($action=="get"){
            return $this->body;
        }

        if($action=="set"){
            $this->body = $value;
        }
    }

    //left side
     public function left_bar_func($action, $value=null){
         if($action=="get"){
            return $this->left_side;
        }

        if($action=="set"){
            $this->left_side = $value;
        }
    }

    //right side
     public function right_bar_func($action, $value=null){
         if($action=="get"){
            return $this->right_side;
        }

        if($action=="set"){
            $this->right_side = $value;
        }
    }

    //menu
     public function menu_func($action, $value=null){
         if($action=="get"){
            return $thi开发者_如何学运维s->menu;
        }

        if($action=="set"){
            $this->menu = $value;
        }
    }

    //between menu and content
     public function menu_content_func($action, $value=null){
         if($action=="get"){
            return $this->menu_content;
        }

        if($action=="set"){
            $this->menu_content = $value;
        }
    }

    //between menu and header
     public function menu_header_func($action, $value=null){
         if($action=="get"){
            return $this->menu_header;
        }

        if($action=="set"){
            $this->menu_header = $value;
        }
    }

}

/*End of file*/


That approach is not much of a templating class. It just collects a few page part snippets as it looks.

Your methods are downright awful, if I may say so. Using $action=="get" and =="set" is even worse than the widespread getter and setter methods which this supposedly emulates. (Making attributes private or protected just because the syntax exists or because of some security or proper coding myth does not make much sense.)
And then calling the methods attribute_func() is likewise uselessly repetetive.

Make it an array. Assign your html snippets there.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号