content – code separation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    content – code separation

    Hi Folks,

    content–code separation is a paradigm many of us know (well, I hope so at least). so my question is: which way do you prefer?

    the absolute way:
    Code:
    <?php
    function makeContent() { /* … */ }
    // some more code
    
    /* printing out the HTML code */
    include "header.htm";
    makeContent();
    ?>
    the template way:
    Code:
    <html>
        <head>…</head>
        <body>
    <?php 
      makeContent();
    ?>
        </body>
    </html>
    the way that currently doesn't come to my mind:
    Code:
    regards
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    The totally abstracted way (more like template way).

    Code:
    [controller]
            -> request data from model 
                    [model]
            <- model return data to controller
    [controller]
            -> create array of returned data
            -> load view, and array of data
                    [view]
                            -> use loaded variables
    That's the best way I can explain codeigniter..

    I do load the header and footer before and after the main view (content), respectively, but the main view is already structured html just awaiting the content passed from the controller.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      but how do you put that into code/markup?

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by Dormilich
        but how do you put that into code/markup?
        I don't, hehe. I let CodeIgniter do it for me. It's out of my know-how, tbqh.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          well, at some point you have to tell the parser, what to do actually. (ok, I didn't dive into the CodeIgniter system....)

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            But, if I were to elaborate, and give you an example:


            Code:
            //Class 'example.php' :
            // Controller is the super object - all our controllers must extend it.
            class Example extends Controller 
            {
                public function Example( )
                {
                    // init the parent's constructor.
                    parent::Controller();
                    // Load my model, with alias.
                    $this->load->model( 'Pm_model', 'MailModel' );
                }
                public function inbox( )
                {
                    // Grab some data from the model, assign it to a variable (in array).
                    // Maybe use a where clause
                    $this->db->where( 'pm_to', $this->session->userdata( 'username' ) );
                    $vars['mail_meta'] = $this->MailModel->grab_mail_meta( );
                    // Load the view, second param: variables for use in view.
                    $this->load->view( 'example', $vars );
                }
            }
            
            // Model: Pm_model.php
            class Pm_model extends Model
            {
                // call parent constructor, etc.
            
                public function grab_mail_meta( )
                {
                    // grab the data.
                    // return array, object, etc.
                    return $data_array;
                }
            }
            
            // View: example.php
            <table>
              <?php foreach ( $mail_meta as $row ) : ?>
              <tr>
                <td><?php echo $row['id']; ?></td>
              </tr>
              <?php endforeach; ?>
            </table>
            That's a barebones example.

            I do suggest CodeIgniter as a framework. It's wonderful.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Ah, I see. so in the end you end up with quite a lot of PHP code in the view, but safe a lot of work on the PHP programming part.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by Dormilich
                Ah, I see. so in the end you end up with quite a lot of PHP code in the view, but safe a lot of work on the PHP programming part.
                Sure, but it's not too much PHP, just the basic looping through arrays, echoing variables, etc.

                Comment

                • TheServant
                  Recognized Expert Top Contributor
                  • Feb 2008
                  • 1168

                  #9
                  I kind of do a mixture of the two. Like so:
                  Code:
                  <?php
                  $var1 = $_GET['something'] * 10;
                  include('other_vars.php');
                  $asd = 23*$var32;
                  ...
                  ?>
                  <body>
                  <div>
                  <p>This is a number: <?php echo($asd); ?></p>
                  </div>
                  </body>
                  So basically do all my PHP first, calcs, includes, etc, and then echo any output below in the html. I have used both your absolute and template, and I guess I swapped between them depending on how much PHP and how much HTML I had per page. Because I really want consistency across my site, I made a hybrid :P

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    to put in my style:
                    Code:
                    <?php
                    // script loader
                        require_once 'load.main.php';
                    
                    // try output class
                    try {
                        $kbl = new HTMLplus("main");
                    }
                    catch (Exception $me)
                    {
                        trigger_error($me->getMessage() . $me->getTraceAsString(), E_USER_ERROR);
                    }
                    // start content output/layout
                        $kbl->Head();
                        $kbl->Titel();
                        $kbl->Meta();
                        $kbl->CSS();
                        $kbl->JS();
                    ?>
                        </head>
                        <body>
                    <!-- some static HTML -->
                    <?php
                        $kbl->Navi();
                        $kbl->Inhalt(); // ⇐ that's the content
                    ?>
                    <!--  more static HTML  -->
                    <?php
                    // include FF and W3C banner
                        $kbl->include("banner");
                    ?>
                        </body>
                    </html>

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      In response to Dormi:

                      The way I've been taught on OOP is that object methods should return data, not output it, which is how it seems you are doing it.

                      TheServant:

                      You use procedural programming, and it's quite hard to maintain/read. However, I think this is where all newbies start out (I'm not calling you a newbie; read on) and I can see you're looking at moving to OOP. One thing I do suggest you do is to, once you're comfortable with php/oop syntax (and methodology, although you will pick this up through) use a framework. I promise this is the last time today I will say this, but CodeIgniter really is awesome :D I'm not pushing you to use it, but frameworks make deploying large applications very efficient and quick.

                      Comment

                      • TheServant
                        Recognized Expert Top Contributor
                        • Feb 2008
                        • 1168

                        #12
                        THanks for the advice. Again, being self taught, in a lot of ways I am a newbie. I have not had any structure to my learning and hence learn things as I need them which is why I am struggling to get around to OOP as I haven't REALLY needed it yet. I have already got CodeIgniter, and it is very interesting, but OOP is still taking a lot of time to get used to. Thanks again.

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          the book that helped me a lot is "PHP 5 Power Programming".

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            Originally posted by Markus
                            The way I've been taught on OOP is that object methods should return data, not output it, which is how it seems you are doing it.
                            that's only for the front-end to make it as easy as possible to the HTML Designer. the return values are true/false (if you need a check beyond the error messages)

                            Comment

                            • Markus
                              Recognized Expert Expert
                              • Jun 2007
                              • 6092

                              #15
                              Originally posted by TheServant
                              THanks for the advice. Again, being self taught, in a lot of ways I am a newbie. I have not had any structure to my learning and hence learn things as I need them which is why I am struggling to get around to OOP as I haven't REALLY needed it yet. I have already got CodeIgniter, and it is very interesting, but OOP is still taking a lot of time to get used to. Thanks again.
                              You're exactly in the same position as me. I just learned things as I needed them, and I still do in fact. It's probably a mindset I'll have for a long time, unfortunately. You'll get it eventually, though. Practice makes perfect. :D

                              Comment

                              Working...