<?php echo … ?> a better way?
-
I just undertook my first project using WordPress. I must admit it’s a bit jarring returning to PHP after over a year of Ruby (but that’s a different topic…).
One thing I am struck by is the sheer amount of HTML mixed with code. This seems a recipe for broken markup. To that end, I’ve created a small DSL (kinda) that should create correct XHTML markup via PHP. Example usage:
<?php
$x = new Xhtml('div', null, Array('class' => 'wrapper'));
$img = $x->add('img', null, Array('src' => 'zoo.jpg'));
$ul = $x->add('ul');
foreach($foos as $foo) {
$ul->add_with_text('li', $foo);
}
echo $x->to_html();
?>
Note that I didn’t have to close any (X)HTML tags or worry about whether the tag was self-closing? Yeah.
My question is this: Would anyone use such a magic tool? If so, I’ll post it.
- The topic ‘<?php echo … ?> a better way?’ is closed to new replies.