在html和php中复制静态内容的当前标准


The current standard to replicate static content in html and php

我知道<?php include 'filename.php';?>可以用于跨多个页面复制静态内容。

我以前为一些以前的网站做过这样的工作,但我正在努力制作一个更专业的网站,我不确定在很多<?php include周围散布是不是一个好方法,感觉就像胶水一样。

专业人士是这样做的吗?我之所以这么问,是因为我所有的页面最终都有一个include作为标题、页眉、页脚和导航。

如果没有更好或更干净的方式,我会继续,但如果有更好的方式来管理这件事,我想知道。

干杯

许多专业人士使用模板库。这是一种更面向对象的方式来做你正在谈论的事情。这个想法是,您可以将内容分解为更小的可重复使用的部分,以及一些添加动态内容的方法,然后构建一个网页就非常简单了。

如果您想推出自己的php类,请考虑制作如下所示的php类:

<?php
class Link
{
public $relation;
public $type;
public $href;
public function __construct($relation, $type, $href)
{
    $this->relation = $relation;
    $this->type = $type;
    $this->href = $href;
}
public function __toString()
{
    return "<link rel='$this->relation' type='$this->type' href='$this->href'>";
}
}
class Anchor
{
public $href;
public $content;
public function __construct($href, $content)
{
    $this->href = $href;
    $this->content = $content;
}
public function __toString()
{
    return "<a href='$this->href'>$this->content</a>";
}
}
class Script
{
public $source;
public $type;
public function __construct($source, $type)
{
    $this->source = $source;
    $this->type = $type;
}
public function __toString()
{
    return "<script src='$this->source' type='$this->type'></script>";
}
}
class Option
{
public $value;
public $text;
public function __construct($value, $text)
{
    $this->value = $value;
    $this->text = $text;
}
public function __toString()
{
    return "<option value='$this->value'>$this->text</option>";
}
}
class Select
{
public $options;
public $name;
public function __construct($name, $options)
{
    $this->name = $name;
    $this->options = $options;
}   
public function __toString()
{
    $result = "<select id='$this->name'>";
    foreach($this->options as $option)
        $result.=$option;
    $result.= "</select>";
    return $result;
}
}
class Div
{
public $id;
public $class;
public $content;
public function __construct($id, $class, $content)
{
    $this->id = $id;
    $this->class = $class;
    $this->content = $content;
}
public function __toString()
{
    return "<div id='$this->id' class='$this->class'>$this->content</div>";
}
}

class HeaderItem
{
public $title;
public $links;
public function __construct($title, $links)
{
    $this->title = $title;
    $this->links = $links;  
}
public function __toString()
{
    $result = "<head>"."<title>$this->title</title>";
    foreach($this->links as $link)
        $result.= $link;
    $result.= "</head>";
    return $result;
}
}
class Document
{
public $header;
public $body;
public function __construct($header, $body)
{
    $this->header = $header;
    $this->body = $body;
}
public function __toString()
{
    return "<!DOCTYPE HTML>$this->header<html><body>$this->body</body></html>";
}
}
?>

了解MVC编程。

包含通常是指在文档中包含页眉或页脚等的方式。但在大型项目中,您通常会创建一个包含标头和任何动态信息的类。然后它将返回解析后的结果。

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

正如@AdamTester所建议的那样,MVC(模型、视图、控制器)无疑是一条发展之路。你可以使用你的模型来处理数据-从数据库等加载数据或你想做的任何事情。控制器允许你做任何你需要做的事情,并使用视图输出结果内容。使用Smarty ,您可以轻松实现您想要做的事情

我当然建议使用Smarty或类似的模板引擎来处理您的视图,因为您也可以按顺序分割不同的视图。