我有一个日历类,该类输出带有表单元格的HTML表,该表单元格代表一个月中的某天,但是我应该将HTML嵌入类文件中。我担心的是,如果我不得不修改HTML(即为元素添加ID),那么我将不得不调整类文件。

我目前在我的项目中不使用MVC模式,因此无法选择 View 。

我削减的类(class)文件如下(对于本示例,我假设1个月为4周):

class calendar {

function __construct(){

}

function output() {
    print "<table>";
    for ($week=0; $week < 4; $week++) {
        print "<tr>";
        for ($day=0; $day < 7; $day++) {
            print "<td></td>";
        }
        print "</tr>";
    }
    print "</table>";
}

还有其他我没想到的方法可以使HTML与类文件分开吗?
提前致谢

最佳答案

最简单的模板

// myTemplate.phtml
<div><?php echo $xy; ?></div>

// SomewhereElse.php
class MyClass {
  public function myMethod () {
    $xy = 'something';
    include('myTemplate.phtml');
  }
}

关于php - 将html嵌入php类文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6123893/

10-14 14:17
查看更多