【问题标题】:PHP Custom templating class [closed]PHP自定义模板类[关闭]
【发布时间】:2023-03-13 03:49:01
【问题描述】:

自从我决定重新开始并编写一个不断增长的 MVC 之后,我虽然拥有一个模板类会很酷。现在,除了简单的包含类型的模板之外,我从未做过任何复杂的模板。现在我写了这个基类,下面解释我的概念。这是编写网页模板的正确方式/概念吗?

概念 “任何模板类都将扩展该类。控制器将修改单个部分或全部。如果控制器仅使用设置操作修改内容,则页面将仅更改一个组件,其他部分将保持不变,就像包括页眉/页脚一样会做”

另一个问题是,我如何传递相同的页面对象,以便将未更改的部分缓存在对象中(因为 http 是无状态协议)?我应该开始会话并将其存储在会话中吗?父母应该是单身人士吗? 谢谢!

<?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 $this->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*/

【问题讨论】:

  • 重新开始。查看现有的模板系统,然后重新开始。
  • 评论从哪里开始(推荐链接或其他内容)比仅仅说“重新开始”更有用
  • Google is a good place to start. 找到这些模板系统,看看他们是如何做到的。决定你喜欢什么,而不是几个。然后重新开始。您上面提供的代码几乎包含代码可能出现的所有问题。从糟糕的 cmets,到重新实现损坏的 getter/setter 概念,再到实际上对模板没有用处。
  • 谢谢,让我看看链接!

标签: php model-view-controller oop templates


【解决方案1】:

这种方法不是模板类。它只是收集了看起来的几个页面部分sn-ps。

如果我可以这么说的话,你的方法非常糟糕。使用$action=="get" 和=="set" 甚至比它所模拟的广泛使用的getter 和setter 方法还要糟糕。 (仅仅因为存在语法或某些安全性或正确的编码神话而将属性设为私有或受保护没有多大意义。)
然后调用attribute_func()方法同样是无用的重复。

使它成为一个数组。在那里分配你的 html sn-ps。

【讨论】:

  • 感谢您的积极批评。我不明白你所说的“把它变成一个数组。在那里分配你的 html sn-ps”是什么意思。请详细说明一下?
  • 您的整个TemplateBase 可能只是一个ArrayObject。但是,如果您真的只收集并稍后打印原始的 html 部分,那么一个普通的旧 array 就足够了。您需要详细说明应该如何使用/利用它以获得更具体的答案。
  • 这个概念是,当用户第一次加载页面时,它会根据用户模板加载首页。例如,用户可以拥有带有标题菜单侧边栏内容和页脚的类,其他用户可能拥有更多。因此,具体类将根据需要设置这些基础组件,格式化它们将如何呈现。在后续查询中,模板对象只会修改单个或两个部分。
  • 不能说,因为您的实现没有任何特殊逻辑。无论如何,不​​要为它与缓存逻辑而烦恼。只需再次阅读所需的部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
  • 1970-01-01
相关资源
最近更新 更多