【发布时间】: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