tags:

views:

181

answers:

3

I like the HEREDOC syntax, e.g. for edge cases of generated HTML that are not worth putting into a template.

The only thing that annoys me about it, though, is that the content, and the closing marker of a heredoc string adheres to the first column. This screws up nested code layouts:

class myclass 
 { 

    function __construct()
      { 
       $a = some_code();
       $b = some_more_code();
       $x = <<<EOT

line1
line2
line3
line4

EOT;    

        $c = even_more_code();
        $b = still_more_code();
        ...
        ...
        ...

you see what I mean. Now this is probably not solvable using normal HEREDOC. Has anybody worked around this? My wet dream would be to have HEREDOC syntax with automatic indentation. But I guess this is not possible without writing some pre-compiler for the source files.

Am I correct?

A: 

Yes, you're correct.

Amber
+1  A: 

That's a problem I often have too : the code is not well indented when I use heredoc, and I really like heredoc :-(

A "bigger" problem is when you select a whole block of code, press "tab" (or any equivalent in your IDE) to indent it more because you added a condition arround it or anything... And it breaks the heredoc strings : you have to go un-indent them by hand :-(

Unfortunatly, I've never seen any tool like the one you're describing...


A solution, I suppose, would be to put the heredoc string in another file, and include it -- the include like could be indented normally ; but it would also mean one more file to load, which would make the code less clear.

Pascal MARTIN
Yup, including another file brings too much confusion... I'm thinking about building a "pre-compiler" like LESS for CSS, but it seems too much of an effort (and an extra step) for too little gain. Too bad!
Pekka
Using some kind of "pre-compiler" would also mean that the code you write and see *(in your IDE/editor)* is not the code that gets executed -- and this will be source of confusions, one day or another...
Pascal MARTIN
+1  A: 

Heredoc is pretty useless in production code. Not just for the reason you state, but also because you're mixing presentation content with business logic.

If you're already using a MVC approach, then why not just stick with it? Put the heredoc text into a view script, bind variables to its scope, and then render it. When you look back a year from now, you'll thank yourself.

mehaase