Development making PHP look nice?

Discussion in 'Software' started by ch424, 23 Oct 2008.

  1. ch424

    ch424 Design Warrior

    Joined:
    26 May 2004
    Posts:
    3,112
    Likes Received:
    41
    Hi people

    I have the code:

    PHP:
    $join date('F Y',$user->join_date);
    $content = <<<HTML
    <div id='userinfo'>
        <h1>User profile for 
    {$user->name}</h1>
        <p>Post count: 
    {$user->num_posts}</p>
        <p>Join date: 
    $join</p>
    </div>
    HTML;
    However, once I get more things like $join, it'll become a confusing mess. Is it possible to do something like this?

    PHP:
    $content = <<<HTML
    <div id='userinfo'>
        <h1>User profile for 
    {$user->name}</h1>
        <p>Post count: 
    {$user->num_posts}</p>
        <p>Join date: {date('F Y',
    $user->join_date)}</p>
    </div>
    HTML;
    This would be so much better, but I can't find any mention of it in the PHP manual. Is there an elegant way to achieve this?

    Thanks
    ch424
     
  2. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    Honestly, it's pretty hard to make any of the languages look nice when you start doing anything beyond the most basic (1st example) stuff. The accepted best way to do things is MVC where your variables are assigned/computed (C) well away from the markup (V).

    So that's sort of a "no" to your question - certainly you can't directly do what you've posted and I can't think of a way to get particularly close to it in a nice way :)
     
  3. ch424

    ch424 Design Warrior

    Joined:
    26 May 2004
    Posts:
    3,112
    Likes Received:
    41
    Thanks for the quick reply. It's a shame.. you can do it this way:

    PHP:
    $content "
    <div id='userinfo'>
        <h1>User profile for 
    {$user->name}</h1>
        <p>Post count: 
    {$user->num_posts}</p>
        <p>Join date: "
    .date('F Y',$user->join_date)."</p>
    </div>"
    ;
    But ...".f()."... isn't as nice as ...{f()}... .

    I do know about MVC. $user is my Model, and $user->join_date is the complete date. Surely it's up the the View to display only the month and year?
     
  4. koola

    koola Minimodder

    Joined:
    11 Jul 2004
    Posts:
    2,401
    Likes Received:
    10
    MVC is the best way tbh. If you did follow the MVC design pattern, then the vars would be passed through your controller into the view through an array.

    Once in your view, you can use PHP short tags (If enabled) to make it look nice.
     
  5. ch424

    ch424 Design Warrior

    Joined:
    26 May 2004
    Posts:
    3,112
    Likes Received:
    41
    That's what I've done, except I used an object rather than an associative array.
     
  6. koola

    koola Minimodder

    Joined:
    11 Jul 2004
    Posts:
    2,401
    Likes Received:
    10
    Then why assign HTML to a var in the view. Just use short tags in with the markup like:

    PHP:
    <div id='userinfo'>
        <h1>User profile for <?=$user->name;?></h1>
        <p>Post count: <?=$user->num_posts;?></p>
        <p>Join date: <?=date('F Y',$user->join_date);?></p>
    </div>
    It's fine to pass objects, but I always end up passing the vars I need through an associative array to make the view even cleaner like so:

    Controller
    PHP:
    $page->view("user_profile", array('uname'=>$user_name,'posts'=>$user->num_posts,'date'=>date('F Y',$user->join_date));
    View
    PHP:
    <div id='userinfo'>
        <h1>User profile for <?=$uname;?></h1>
        <p>Post count: <?=$posts;?></p>
        <p>Join date: <?=$date;?></p>
    </div>
    Neither way is incorrect but I prefer the latter for better readability and less PHP in the view.
     
  7. ch424

    ch424 Design Warrior

    Joined:
    26 May 2004
    Posts:
    3,112
    Likes Received:
    41
    Cool, I see what you mean, thank you :)

    How about this though: say I have an optional "about me" box. For example:
    PHP:
    <div id='userinfo'>
        <h1>User profile for <?=$uname;?></h1>
        <p>Post count: <?=$posts;?></p>
        <p>Join date: <?=$date;?></p>
        <?=($about?"<p>About me: $about</p>":"");?>
    </div>
    This would draw the "about me" <p> but draw nothing if $about was empty. Is there a more 'proper' way of doing this?

    Edit: also, how would you do a loop in that? for example, if you had many comments and were drawing the same thing for each one?

    eg
    PHP:
    <div id='userinfo'>
        <h1>User profile for <?=$uname;?></h1>
        <p>Post count: <?=$posts;?></p>
        <p>Join date: <?=$date;?></p>
    </div>
    <div id='recentcomments'>
    <h2>Recent comments</h2>
    <?php foreach ($comments as $comment) {?>
        <h3><?=$comment['title'];?> &mdash; <?=$comment['datetime'];?></h3>
        <p><?=$comment['content'];?></p>
    <?php ;}?>
    </div>
    That won't work in PHP (it would in ASP, which is where I've come from and why I'm asking all these php questions), so what can I do instead?

    Thank you
    ch424
     
    Last edited: 23 Oct 2008
  8. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    Well not really, you're still going to have to check for $about's existence/status. Just doing "if($about)" is a bit too ambiguous for me, if it was me i'd make $about only exist if it's going to be used.

    PHP:
    <?php if (isset($about)): ?>
      <p><?php echo $about?></p>
    <?php endif ?>
    Why not?

    Btw, do it using this style (i forget the real name)

    PHP:
    <ul>
      <?php foreach ($foo as $bar): ?>
        <li><?php echo $bar ?></li> 
      <?php endforeach ?>
    </ul>

    Same for ifs, whiles, etc.
     
  9. koola

    koola Minimodder

    Joined:
    11 Jul 2004
    Posts:
    2,401
    Likes Received:
    10
    nps :)

    You can insert conditionals with short tags too:

    PHP:
    <div id='userinfo'>
        <h1>User profile for <?=$uname;?></h1>
        <p>Post count: <?=$posts;?></p>
        <p>Join date: <?=$date;?></p>
        <? if (!empty($about)):?>
        <p>About me: $about</p>
        <? endif;?>
    </div>
    OR

    PHP:
    <div id='userinfo'>
        <h1>User profile for <?=$uname;?></h1>
        <p>Post count: <?=$posts;?></p>
        <p>Join date: <?=$date;?></p>
        <?=(!empty($about))?"<p>About me: ".$about."</p>":"";?>
    </div>
    Edit: Good examples of short tags
     
    Last edited: 23 Oct 2008
  10. ch424

    ch424 Design Warrior

    Joined:
    26 May 2004
    Posts:
    3,112
    Likes Received:
    41
    Oooooh it does work! Thank you both! :D

    And thanks for the link too koola!

    *is happy*

    ch424
     
  11. BentAnat

    BentAnat Software Dev

    Joined:
    26 Jun 2008
    Posts:
    7,230
    Likes Received:
    219
    Damnit... beat to the punch. Was about to write this long essay about MVC and all of that... nice work.
     
  12. Firehed

    Firehed Why not? I own a domain to match.

    Joined:
    15 Feb 2004
    Posts:
    12,574
    Likes Received:
    16
    Mind your settings for short/ASP-style tags though, since those are often off by default on servers. It'll confuse the hell out of you if you ever migrate to a different site.

    Though you may be able to override the php.ini setting on the fly with the ini_set() command, which I'd do by default to be safe.

    The other option is to make your own sort of rudimentary templating system, which would look for words contained in curly braces included in some sort of markup parser and grab their values out of some sort of associative array. What you have is easier by far, though somewhat less maintainable. I think that a lot of the templating frameworks (at least for PHP) are a bit overly complex and heavy, but having done some brief work in ASP.NET as well I have to give MS a ton of credit for Master Pages, which are absolutely brilliant (only part of the thing that I like or even appreciate, but I've been trying to find a good way to duplicate them in PHP ever since). They're not entirely different from the above, but there's a certain elegance in the way they're executed.

    Actually I may be doing some more work in that area soon enough so if I make any progress on it I'll probably opensource the system and post up a link.
     
  13. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    Yes. This is why I decided to stick with <?php echo $foo ?> style, and also it'll never cause problems if someone has configured another language to use <? tags :)

    Agh, as nice as some of these systems are, I still can't help but feel that building a templating system using a templating language is dumb :D
     
  14. BentAnat

    BentAnat Software Dev

    Joined:
    26 Jun 2008
    Posts:
    7,230
    Likes Received:
    219
    http://code.google.com/p/keepr/
    No downloads yet, but i'm one of the (2 i think) people actually contributing to this.
    Strong on the MVC part, even though it's there that i did a lot of work.

    I've used it on a number of sites so far, and it does the job well... saves me some time. Problem to me was wrapping my head around it at first...
    Uses xtemplate for templating, which mostly does the job.
     
  15. Jamie

    Jamie ex-Bit-Tech code junkie

    Joined:
    12 Mar 2001
    Posts:
    8,180
    Likes Received:
    54
    Look at smarty if you want to use a templating system that is a bit cleaner and more powerful than the native php tag way of doing things.

    And please note you don't need semi-colons everywhere when using short tags and ending blocks.
     

Share This Page