-1

Is there an standard output library that "knows" that php outputs to html?

For instance:

  1. var_dump - this should be wrapped in <pre> or maybe in a table if the variable is an array
  2. a version of echo that adds a "<br/>\n" in the end
  3. Somewhere in the middle of PHPcode I want to add an H3 title:

.

?><h3><?= $title ?></h3><?

Out of php and then back in. I'd rather write:

tag_wrap($title, 'h3');

or

h3($title);

Obviously I can write a library myself, but I would prefer to use a conventional way if there is one.

Edit

3 Might not be a good example - I don't get much for using alternative syntax and I could have made it shorter.
1 and 2 are useful for debugging and quick testing.
I doubt that anyone would murder me for using some high-level html emitting functions of my own making when it saves a lot of writing.

sabof
  • 8,062
  • 4
  • 28
  • 52

2 Answers2

3

In regards to #1, try xdebug's var_dump override, if you control your server and can install PHP extensions. The remote debugger and performance tools provided by xdebug are great additions to your arsenal. If you're looking only for pure PHP code, consider Kint or dBug to supplement var_dump.

In regards to #2 and #3, you don't need to do this. Rather, you probably shouldn't do this.

PHP makes a fine HTML templating language. Trying to create functions to emit HTML is going to lead you down a horrible road of basically implementing the DOM in a horribly awkward and backwards way. Considering how horribly awkward the DOM already is, that'll be quite an accomplishment. The future maintainers of your code are going to want to murder you for it.

There is no shame in escaping out of PHP to emit large blocks of HTML. Escaping out to emit a single tag, though, is completely silly. Don't do that, and don't create functions that do that. There are better ways.

First, don't forget that print and echo aren't functions, they're built in to the language parser. Because they're special snowflakes, they can take a list without parens. This can make some awkward HTML construction far less awkward. For example:

echo '<select name="', htmlspecialchars($select_name), '</select>';
foreach($list as $key => $value) {
    echo '<option value="',
         htmlspecialchars($key),
         '">',
         htmlspecialchars($value),
         '</option>'
}
echo '</select>';

Next, PHP supports heredocs, a method of creating a double-quoted string without the double-quotes:

$snippet = <<<HERE
<h1>$heading</h1>
<p>
    <span class="aside">$aside_content</span>
    $actual_content
</p>    
HERE;

With these two tools in your arsenal, you may find yourself breaking out of PHP far less frequently.

While there is a case for helper functions (there are only so many ways you can build a <select>, for example), you want to use these carefully and create them to reduce copy and paste, not simply to create them. The people that will be taking care of the code you're writing five years from now will appreciate you for it.

Charles
  • 50,943
  • 13
  • 104
  • 142
  • I'll give xdebug a try, and keep your advice in mind. I posted because I find it strange that PHP tries to be langage-agnostic, while it is used for one thing and one thing only - programs that emit (x)html. – sabof Feb 15 '12 at 02:14
  • Considering PHP's origin as an HTML preprocessor, it's not surprising that it provides many tools to make emitting HTML easier. Over the years, it's gained the ability to produce everything from images to PDFs to Excel documents to partridges in pear trees. That's far from one thing only. – Charles Feb 15 '12 at 02:17
  • Without exaggeration, it's still the vast majority of it's use. I would expect lots of HTML-only functionality. write-vector-to-ul write-array-to-table. This is something done all the time – sabof Feb 15 '12 at 02:24
  • 1
    I can't understand the downvotes...this answer looks perfectly fine to me. – Christian Feb 15 '12 at 08:32
1

You should use a php template engine and just separate the entire presentation and logic. It make no sense for a educated programmer to try to create a library like that.

Neo
  • 11,078
  • 2
  • 68
  • 79
  • 1
    SMARTY is my favorite I was gonna give the same answer. I feel bad for programmers that still echo things in php. I don't really consider them programmers if they don't get the whole separation of concerns even if they don't like MVC. –  Feb 15 '12 at 03:03
  • 1
    You do not *need* a template engine just to establish separation of concerns. [PHP is a perfectly adequate template engine in itself](http://massassi.com/php/articles/template_engines/). – Charles Feb 15 '12 at 04:14
  • 1
    @Charles yes and smarty is a template engine made by php. It is really just php code, but suited for writing HTML code. I did not say "separation of concerns" , the person who commented did. I suggested smarty to this person because it would be easier for them to use with their level of php instead of bootstraping all by them-self. – Neo Feb 15 '12 at 04:38
  • FWIW, "separation of concerns" is the same idea as "just separate the entire presentation and logic." – Charles Feb 15 '12 at 04:59
  • I was just saying that was not even the word I used. I didn't say you need a template engine for separation of concern. I said you should use a template engine and that way you don't have to be concerned about closing and opening php tags 20 times in one line "and just separate the entire presentation and logic". Not "inorder to separate the entire presentation and logic you have to do this". Don't take it so hard! – Neo Feb 15 '12 at 05:12
  • It's a programming approach subject. Some get religious, others just defensive. I'd rather use core php if possible, if there is a lot of intertwining between html and data preparation, SMARTY looks interesting. – sabof Feb 15 '12 at 05:33
  • 1
    @Charles PHP the template engine blabla. Are you telling car drivers that they don't need to drive cars as their legs will just take them anywhere, as well? Smarty is a perfectly fine template engine and you can create some really nice and clear looking templates. And yes, that's what it's all about: Good looking templates. If you prefer mixing PHP and HTML, nobody is stopping you to write your own answer. – Basti Mar 13 '12 at 22:04