6

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

3
  • 1
    include the calendar class in a php file (index.php), and use: $cal = new Calendar(); , now you can do this: $cal->week; (depends on how you wrote the class), maybe 'print_r($cal)' first Commented May 25, 2011 at 11:45
  • maybe check this question : stackoverflow.com/questions/62617/… Commented May 25, 2011 at 11:46
  • This question does not fit the embedded tag see stackoverflow.com/tags/embedded/info. Tag removed Commented May 25, 2011 at 14:50

4 Answers 4

3

The most simple templating

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

// SomewhereElse.php
class MyClass {
  public function myMethod () {
    $xy = 'something';
    include('myTemplate.phtml');
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.