Re: Auto-generating HTML

From: Date: Mon, 20 Sep 2010 20:17:01 +0000
Subject: Re: Auto-generating HTML
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-308222@lists.php.net to get a copy of this message
On Mon, Sep 20, 2010 at 3:59 PM, Peter Lind <peter.e.lind@gmail.com> wrote: > On 20 September 2010 21:56, Andy McKenzie <amckenzie4@gmail.com> wrote: >> On Mon, Sep 20, 2010 at 3:50 PM, Rick Pasotto <rick@niof.net> wrote: >>> On Mon, Sep 20, 2010 at 03:02:35PM -0400, TR Shaw wrote: >>>> >>>> On Sep 20, 2010, at 2:56 PM, Andy McKenzie wrote: >>>> >>>> > Hey folks, >>>> > >>>> >  I have the feeling this is a stupid question, but I can't even find >>>> > anything about it.  Maybe I'm just not searching for the right things. >>>> > >>>> >  Here's the problem.  I'm writing a lot of pages, and I hate going >>>> > in >>>> > and out of PHP.  At the same time, I want my HTML to be legible.  When >>>> > you look at it, that's kind of a problem, though... for instance >>>> > (assume this had some PHP in the middle, and there was actually a >>>> > reason not to just put this in HTML in the first place): >>>> > >>>> > Simple PHP: >>>> > <?php >>>> > >>>> > echo '<html>'; >>>> > echo '<head>'; >>>> > echo '  <title>Page Title</title>'; >>>> > echo '</head>'; >>>> > echo '<body>'; >>>> > echo '<p>This is the page body</p>'; >>>> > echo '</body>'; >>>> > echo '</html>'; >>>> > >>>> > ?> >>>> > >>>> > >>>> > Output page source: >>>> > <html><head>  <title>Page >>>> > Title</title></head><body><p>This is the >>>> > page body</p></body></html> >>>> > >>>> > >>>> > Now, I can go through and add a newline to the end of each line (echo >>>> > '<html>' . "\n"; and so on), but it adds a lot of >>>> > typing.  Is there a >>>> > way to make this happen automatically?  I thought about just building >>>> > a simple function, but I run into problem with quotes -- either I >>>> > can't use single quotes, or I can't use double quotes. >>>> >  Historically, >>>> > I've dealt with the issue by just having ugly output code, but I'd >>>> > like to stop doing that.  How do other people deal with this? >>>> > >>>> > Thanks, >>>> >  Alex >>>> >>>> Alex >>>> >>>> Just add a \n at the end as >>>> >>>> echo '<html>\n'; >>> >>> That will not work. Single quotes means that the '\n' is not interpreted >>> as a new line so you'll see a bunch of '\n' in the output. >>> >>> What I sometimes do is: >>> >>> $out = array(); >>> $out[] = '<html>'; >>> $out[] = '<head>'; >>> $out[] = '  <title>Page Title</title>'; >>> $out[] = '</head>'; >>> $out[] = '<body>'; >>> $out[] = '<p>This is the page body</p>'; >>> $out[] = '</body>'; >>> $out[] = '</html>'; >>> echo join("\n",$out); >>> >> >> Interesting.  I hadn't thought of that, but it could work.  It'd still >> be quite a bit of extra typing, but at least I find it more >> readable... >> > > Ash already mentioned it: heredoc format. Much easier, less typing, > easier to read, keeps formatting, etc, etc etc. > > Regards > Peter > > -- > <hype> > WWW: http://plphp.dk / > http://plind.dk > LinkedIn: http://www.linkedin.com/in/plind > BeWelcome/Couchsurfing: Fake51 > Twitter: http://twitter.com/kafe15 > </hype> > You may well be right; it's a format I haven't used much, but it may well be the right choice here. I think the main thing I'm seeing is that there isn't a single, accepted, simple way to do this: no matter what I do, it will be a workaround of some type. Either I'm adding complexity (a function to convert everything), or I'm adding lines (heredoc/nowdoc seem to require that the opening and closing tags be on lines without any of the string on them), or I'm adding typing (adding ' . "\n"' to the end of every line of HTML). Perhaps I'll put some effort into building a function to do it, but not this week... I think for now I'll keep appending those newlines, and just have more code to fix at a later date. It's reasonably clean, it's just mildly annoying. Thanks, all, and if anyone comes up with a really elegant solution, please let me know! -Alex

« previous php.general (#308222) next »