How to handle HTML in code
Posted: Thu Feb 28, 2008 5:26 pm
First off I will start with a short introduction of myself since I'm new to this board. I'm a twenty year old guy from Sweden with three years of experience in PHP programming. I'm currently working as an application engineer for a bigger website.
I recently started to think about the problem (from my view) in printing HTML from PHP. What do I mean when I say this is problem? Well, we all know that reading and writing HTML code in a PHP document is a pain. And the indentions and whitespace will look really awful and be out of control when executed.
So how can we solve this? There are several templating systems out there, but they have too much rules and are often big and complex. I want to write all my loops and code in PHP. My idea was to create a couple of functions to help create HTML tags and then keep track of indention and automatically handle line breaks. The functions will be written in C as a Zend module. Here is some example code:
I have made a functional copy of this as a Zend module but have discovered a problem:
In my example we had a function named text() with a argument executing another function:
text("p", "If you want to create a new account, please " . hyperlink("click here", "/create", "Create a new account"));
PHP will first execute hyperlink() and append its output to the string and then execute text(). As mentioned earlier my functions automaticly indents and add line breaks. This is a problem - I don't want a indention or line break inside my paragraph. One way to solve this problem is to add an extra argument: hyperlink("click here", "/create", "Create a new account", TRUE);. TRUE will tell the function to not indent or break line, but this is not a neat solution.
So my question is, can I in some way check if my function is passed as a argument to another function?
I'm glad for any toughts or ideas about all of this.
I recently started to think about the problem (from my view) in printing HTML from PHP. What do I mean when I say this is problem? Well, we all know that reading and writing HTML code in a PHP document is a pain. And the indentions and whitespace will look really awful and be out of control when executed.
So how can we solve this? There are several templating systems out there, but they have too much rules and are often big and complex. I want to write all my loops and code in PHP. My idea was to create a couple of functions to help create HTML tags and then keep track of indention and automatically handle line breaks. The functions will be written in C as a Zend module. Here is some example code:
Code: Select all
<?php
tag("body");
tag("div", array("class" => "info"));
text("p", "If you want to create a new account, please " . hyperlink("click here", "/create", "Create a new account"));
tag();
tag("div", array("class" => "login"));
form("login", "/login.php", GET);
box("uid", "134680", HIDDEN);
box("key", "aNmMAF", HIDDEN);
box("username", NULL, TEXT);
box("password", NULL, PASSWORD);
img("/images/validate.png", "Type this text in the box below", 50, 200);
box("validate", NULL, TEXT);
button("Submit form", SUBMIT);
form();
tag();
tag();
?>Code: Select all
<body>
<div class="info">
<p>If you want to create a new account, please <a href="/create" title="Create a new account">click here</a></p>
</div>
<div class="login">
<form name="login" action="/login.php" method="GET">
<input type="hidden" name="uid" value="134680" />
<input type="hidden" name="key" value="aNmMAF" />
<input type="text" name="username" value="" />
<input type="password" name="password" value="" />
<img src="/images/validate.png" alt="Type this text in the box below" height="50" width="200" />
<input type="text" name="validate" value="" />
<input type="submit" value="Submit form" />
</form>
</div>
</body>
In my example we had a function named text() with a argument executing another function:
text("p", "If you want to create a new account, please " . hyperlink("click here", "/create", "Create a new account"));
PHP will first execute hyperlink() and append its output to the string and then execute text(). As mentioned earlier my functions automaticly indents and add line breaks. This is a problem - I don't want a indention or line break inside my paragraph. One way to solve this problem is to add an extra argument: hyperlink("click here", "/create", "Create a new account", TRUE);. TRUE will tell the function to not indent or break line, but this is not a neat solution.
So my question is, can I in some way check if my function is passed as a argument to another function?
I'm glad for any toughts or ideas about all of this.