SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 35 of 35

Thread: coding standard

  1. #26
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    *has a fit when code doesn't match my style, and goes on a rampage to reformat it -.-*
    lol - same here. I'm always getting emails asking to fix overly-complicated code, and that's the first thing I do - turn it to my style.

    Luckily, though, my style is faster and uses up less lines. lol again.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  2. #27
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    If you prefer to mix code/presentation, go ahead.

    There are times when separating code just makes life more compicated.
    That's like recommending not using stylesheets and embeding all the style in the HTML elements.

  3. #28
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman View Post
    That's like recommending not using stylesheets and embeding all the style in the HTML elements.
    Not really, there is a lot of ground in-between there, and we aren't talking about style sheets, but about markup and server side code.

  4. #29
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mixing is fine, but only for small and simple scripts.

  5. #30
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hammer65 View Post
    Not really, there is a lot of ground in-between there, and we aren't talking about style sheets, but about markup and server side code.
    My comments were related to saving mark-up (rather than data) in the database. Is that what you are saying is OK?

  6. #31
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman View Post
    My comments were related to saving mark-up (rather than data) in the database. Is that what you are saying is OK?
    No I would agree with you there. There are some CMS systems that do storage in the database, but I try to keep database activity to a minimum. Getting markup from the db along with everything else you want to do with it, just increases the load. I would vote no on that idea, unless it's a CMS and your getting bits and pieces.

  7. #32
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Exactly - which is why I store output HTML in files.

    In fact, a maybe better approach would be to store all the HTML modules you use in a database, which would completely save the need for any HTML in business logic. (I think).

    For example, if you had a simple shopping cart, you could have the following rows in the table to show it:
    CartHeader
    Code html:
    <table>
    <tr><th>Product Code</th><th>Item Description</th><th>Price</th></tr>
    CartRow
    Code html:
    <tr><td>%1$s</td><td>%2$s</td><td>%3$s</td></tr>
    CartFooter
    Code html:
    </table>
    (Maybe that's a bit of a long-winded approach - it's an example )

    Then you could use the following:
    PHP Code:
    <?
    $getHtml_Top 
    mysql_query("SELECT html_output FROM html WHERE name='CartHeader'");
    $getHtml_Row mysql_query("SELECT html_output FROM html WHERE name='CartRow'");
    $getHtml_Bottom mysql_query("SELECT html_output FROM html WHERE name='CartFooter'");
    $top mysql_result($getHtml_Top0);
    $row mysql_result($getHtml_Row0);
    $bottom mysql_result($getHtml_Bottom);
    echo 
    $top;
    foreach(
    $_SESSION['cart'] AS $item){
       
    printf($row$itemgetDescription($item), getPrice($item));
    }
    echo 
    $bottom;
    it was a bad example, but if your that desperate to separate logic from HTML, it works.
    Nice approach. In an framework based approch, people tend to have the seperation so people with little or moderate knowledge of PHP can still edit a template file.

    Question, is there any way to replace the %1, etc with a var-name?

  8. #33
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Talk about bumping. :S

    Separation isn't just so that other people can edit your layout. Even if you're the only one working on the project, if you feel like changing your layout by moving things around later and you didn't separate your presentation and logic, you'll be crying because of all the work you need to do.

    Personally I wouldn't store templates in a DB, but if you want to and you want to use variable names, you would do the same thing as you do with regular template files. Either parse a templating language or use eval().

  9. #34
    Forum Buyer
    Join Date
    Jun 2004
    Location
    United States
    Posts
    811
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mikemckinney View Post
    My style is to put business logic at the top of the PHP file. Either by using include/require statements or by using code directly. Then the bottom of the file is presentation logic. In most cases, this is HTML which escapes to PHP when needed. This allows me to read the HTML source code and get a better feel of the page layout. Another advantage is that I can see the page layout in a WYSIWYG editor (should I want to do that).

    Just my $0.02...
    mikem
    Cosigned.
    Founder/Admin of a pretty decent chat forum
    Download free winterboard themes for your iPhone
    I run sites powered by vbulletin and one about the HTC Jetstream.

  10. #35
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Wow this thread is old!

    Take everything I said above, and forget it! Firstly, HereDoc is great . Secondly, getting templates out of the database isn't a great way of going about things and thirdly using an MVC approach is much better.

    For example, a template file like the following would be better:
    PHP Code:
    <ul class="Cart">
        <?php
            
    foreach($Sender->Items as $Item){
                
    printf('
                    <li>
                        <h3>%1$s</h3>
                        <p class="Price">%2$s</p>
                        <p class="Description">%3$s</p>
                    </li>
                '
    $Item->GetName(), $Item->GetPrice(GetUser()->GetCurrency()), $Item->GetDescription());
        
    ?>
    </ul>
    Splitting into files creates an annoyance when editing, even though organisation may be a bit better.

    Of course, the example above isn't perfect. However, it just so happens that 'perfect code' really doesn't exist - programming is a constant journey, you always learn new things and your methods can change. This is why this thread wasn't a great one, because it's such an ambiguous topic. Some people have certain ways of doing things, others have others. In the end it comes down to what suits you and what suits the situation.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •