tags:

views:

1073

answers:

12

I'm writing a small web app as a side project. It's done in PHP.

Boy, how I loathe PHP.

Well, actually, I don't hate PHP per se. I can't stand HTML intermixed with code. I can barely look at one of those templates without feeling nauseated. I know, when you have an army of "web designers" at your disposal, and you are the only developer, it makes sense to use some templating system. I heard about Smarty a lot. But in my case it has been always the source of yet more work.

Anyways, this time I'm going solo, so I don't want to touch an HTML template with the proverbial pole. So what I'm currently doing looks like this:

<?php
$page = new html_page('My wonderful page');
$page->add_contents(new html_tag('p', 'It works', array('id' => 'helloworld', 'class' => 'somecssclass')));

echo $page->render();
?>

Everything belongs to a nice hierarchy of objects, which is good and dandy. Of course I have a lot of smaller classes, and I'm thinking of using dynamic classes (for example, 'html_a' will automagically create an html_tag object of type 'a'.)

Now, my question: it seems that nobody else is doing this. Why? I'm too far out there, and my feeble, oxygen deprived mind is missing something from the big picture?

(I clearly remember an open source library that did exactly this, but can't find it anymore. So unless I'm actually imagining things, I'm not the only one who thought of this approach to render HTML.)

Do you have any thoughts on this? (please refrain to recommend another language; my favorite language is Perl, so I can outfan you easily :-). I'm stuck with PHP.)

Edit

Uh-uh, seems that I struck a nerve. Some clarifications:

  1. I'm the only developer in this project.
  2. How I'm mixing code with HTML? An "html_tag" object from my library is pretty similar to, say, a node in the DOM. The "render" method is the one that creates html (beautifully indented, I must add) but I don't write any opening or closing tag anywhere.
  3. I create small objects for several tasks. These objects have methods to build tag objects; these resulting objects are then inserted into, say, tables or pages.
  4. Did I mention that I'm stuck with PHP?
  5. My library have some primitive access methods to find objects. So the iterator example posted in 26288 can be implemented with relative ease.
  6. I'm not worried about performance (yet.) I've always thought that's a really nice problem to have, but I'm not there yet.
+1  A: 

PHP is a template language. therefore, it's natural that all PHP fans are using template-like design.

there are some LISP libraries that do things like you put there. IMHO, this is a big turn-off when i approach LISP languages.

Javier
He's right, that's what PHP does. If its not the correct tool for the job, why muscle it into place?
cfeduke
Hm, because I don't have a choice?Regarding Lisp, quoting Larry Wall, "Lisp has all the visual appeal of oatmeal with fingernail clippings mixed in." I used Scheme in college; I second that statement.
Leonardo Herrera
"Those who don't know Lisp are condemned to reinvent it, poorly". i'm not advocating Lisp, just noting that you've redone the ugliest part of it.
Javier
+9  A: 

Well, the bottom line is, you're still mixing your HTML with your code. If you wanted to change that "p" tag to a "div", you'd have to wander through your code just to do it. Think about what your method offers:

  • mixes code with HTML
  • adds overhead to parse all the requests
  • introduces a new "language" over HTML

In essence, while the approach may be different, it has the same issues the template languages you are trying to get away from has.

Wouldn't it be easier, if you're working alone (or in a group), to just let PHP be the template language?

$page = new Page('test.html');
$page->load($data);
$page->render();

and in your test.html "template"

<html>
<head>
  <title><?php echo $title ?></title>
</head>
<body>
  <p><?php echo $hello ?></p>
</body>
</html>

What other template engines do really is formalize the above code. but if you stick to basics (echoing variables, basic conditionals, looping), you essentially have all the power of a template language, but in familiar PHP, and with no performance overhead.

Plus, unlike your example, you can alter the HTML (i know you didn't want to touch it, but changing a "p" to a "div" in code really isn't that different from change <p> to <div>), without having to delve into the code.

Owen
TITCR, the OP's example of dynamically building html pages by putting function calls with hardcoded html element names is horrible. It makes it extremely difficult to change presentation. Unless you keep all those calls in the same place, but then it's just an inconvenient template language.
Roel
A: 

I know you don't want to hear about templating systems, but if you've only looked at Smarty you're missing the best option. Try Tiny But Strong

www.tinybutstrong.com

Like you I'm the only developer, but I find TBS increases my productivity and the maintainability of my code massively. Unlike smarty it doesn't impose a macro language in the templates and it's completely, 100% compatible with Dreamweaver - the templates really and truly remain as wysiwyg which saves an absolute ton of time.

Being able to write all my code in separate files from the template means it can be formatted and arranged in a standard fashion. Maintenance is a dozy as I always know how my code is structured.

I use TBS with the XAJAX libraries and it works a treat, but you could mix whatever your preferred libraries are with it - I've had it running with Dojo for instance quite nicely and I'm looking at jQuery next up.

Cruachan
A: 

Nobody else is doing it because it's quite a bit more work than just escaping in and out of PHP mode for a template file, not to mention it just adds overhead purely to satisfy your preference for code aesthetics.

This doesn't mean you can separate logic from display, such as a simple system like Owen suggested. But to shy away from the convention of php mixed with HTML just because you don't like it is a little silly - it's how the language was made to be used.

Peter Bailey
You are right in the matter that PHP was initially designed as a template processor. That doesn't mean it is okay to do it.And I don't want to separate things, that's why I'm not using a template based system.
Leonardo Herrera
+2  A: 

I'm not sure what you're talking about. Are you looking for a template library for php? There are plenty, and although it's fashionable to hate on Smarty (just like it's fashionable to hate on php itself), it's a great library.

Are you asking why 'everybody' mixes html in their php code? The answer to that is that only beginnners and idiots do that. Everybody who has real-world php experience uses a template engine.

If you are asking why nobody adds every html tage in code like in your example, well that's obvious: because it's a maintenance nightmare. Are you going to give your code to a designer and tell him good luck with it? Are you going to manually convert every html page to pages and pages of code?

Roel
I second smarty. It's light weight and gets the job done.
Gary Richardson
Considering symfony, it is not even using smarty, yet it has all the functionality. Slots, partials, helpers, etc.
ken
Guys, I don't want to use a template library in the first place. That's what prompted me to ask about this approach.
Leonardo Herrera
The thing is that what you're asking about is very convoluted and painful way of doing what can much easier be done with a template library. Maybe you can enlighten us on *why* you don't want to use a template engine, because right now your question doesn't make much sense.
Roel
the thing is, you have essentially created a template library. it's a layer over HTML, designed to finally output HTML. that goal is no different from conventional template libraries, the approach is just slightly different.
Owen
+2  A: 

So, if I understand correctly, you're saying that you hate having code mixed into your HTML so much that you've decided to mix HTML into your code instead? I fail to see how that improves anything - the code and HTML are still mixed - and it's the less natural/more complex way of approaching PHP, which seems to provide an obvious explanation for why nobody else is doing it.

Use a proper templating system which actually separates the code from the HTML instead of just toying with which one is embedded within the other. I would suggest HTML::Template or Template::Toolkit, since you share my taste for Perl, but you've already said that non-PHP languages aren't viable options.

Dave Sherohman
+1  A: 

Question 261338 has some colinerarity with this, but my response there was more targeted at the php people whom are new to this whole programming "seperation of concerns? whats that?" people. ( sorry php people, largely this is the case )

You could be a good perl player and use perl to generate your html templatey crap that gets loaded in php, I'm still new to perl, but php reeks and I've used it for far too long, surely there has to be a way to write a site in perl that runs on php.

Thirdly, I'd like to see something that does augmentative substitution beyond variables.

Sort of like jQuery style dom matching but without the bloat, java( rhino ), or the whole web browsery thing.

$page = new Page("FakePage.html"); 
$page->find("div#foobar")->text = "Hahah! I rock" ; 
/* Give All H1's a numeric lead in  */  
foreach( $page->find("h1")->iterator() as $index => $node )
{
        $node->text =  ($index + 1 ) . ". " $node.text;
};
$page->render();

I'd really love something that did that nicely and didn't suck too hard. Note my use of structures that probably wont be entirely loved by php.

Dreams are free.

( That is the /only/ syntax that truely seperates design and logic, all the other templatey stuff is just recursively diminishing programming languages )

The extra cool part here is:

  1. No need to santise html, its DOM AWARE!
  2. No need for weird special template markup that sucks and won't work in 90% of editors and can't be validated on its own without bleeding a chicken over it.
Kent Fredric
Ah .. See my answer - It almost reads as a comment to yours.
troelskn
+2  A: 

Slightly different, but yet similar. I wrote an article about using DOM for binding variables to templates. You may find it interesting.

Basic use-case:

$t = new Domling('<p class="hello"></p>'); 
$t->capture('hello')->bind("Hello World"); 
echo $t->render();

Which produces:

<p class="hello">Hello World</p>
troelskn
Not exactly what I have in mind (I don't want to parse a template to get my DOM) but it gave me several ideas.
Leonardo Herrera
+1  A: 

Well, it seems that people got upset about the "loathe PHP" quote. That's not the intended spirit of the question, sorry if I put your favorite language off.

At the end, there are some really valid points made by several people;

  1. The unfortunate naming convention: having the name of the HTML element as the name of the object rings a bell.
  2. I'm reinventing Lisp, and the worst part of it at that.
  3. This is just another template system, and a bad one to boot.

The accepted answer for me was the nice article posted by troelskn. I came into the conclusion that I need my own document model, and that I should refrain to use the html entity names on it. As I said before, this is a small side project; that article and some pointers I got from its comments gave me new ideas to explore.

(About the reinventing Lisp part: I'm the only person I know that enjoyed programming the DOM. So there.)

Thank you all for your comments.

Leonardo Herrera
A: 

There are very little frameworks for php that do what you want. Try Prado (http://www.pradosoft.com/) or Yii (http://www.yiiframework.com/). They support full fledged tag libraries, not just templating systems. You can also check phpframeworks.com, you may find some other framework that does what you need there.

I'm also developing my own framework with support for tag libraries and server-side dom manipulation, but I still need to optimize the code a little bit before publishing anything, this project is under http://players.sourceforge.net.

A: 

I'm using the xml, xsl approach to develop my forms, using a single xls template to render any form I use in my portal. At the beginning it was little hard to find out the right format of xls file, but after it was done, it was very easy to render my forms. In php I just have to create a XML object and render it.

Obviously, there will be 'special' things that I don't want to render with xls, in that cases I render them the normal way.

I don't like to mix to much thing with php code, that is why I developed this libraries...

A: 

I developed a lightweight close to the metal tag lib for many of the same reasons, keeping the flavor Id seen in using lisp to generate HTML.

Overview/sample code here xilla_tags

gord