开发者

Embedding html in php class file

I have a calendar class which outputs a HTML table with a table cell representing a day of the month, however should I be embedding HTML into a class file. My concern is that if I ever have to amend the HTML (i.e add an ID to an element), then I would have to adjust the class file.

I currently dont use the MVC pattern in my project so having a view is not an option.

My cut down class files is as follows (for this example I have assumed that 1 month is 4 weeks):

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>";
}

Are there any other methods which I haven't thought about which would keep the HTML separate from the class file Thanks in advance


The most simple templating

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

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


One very popular solution for your problem is to use http://www.smarty.net/ or order template engine in order to split your presentation logic from business logic.


You could have some sort of HTML helper class that generates your code. The class can have createTable, addRow, closeTable, createForm, addField, etc.. The main properties are sent in the method call, and the constants are coded directly into the HTML.


I would simply write an HTML file and put placeholders there.

<!DOCTYPE HTML>
<html>
...
<?php echo output() ?>
...
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜