ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

best practice for code separation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • best practice for code separation

    Hi all



    i'm looking for examples/suggestion for best practice for NOT mixing Php, Html, css ecc. in the same file so that to keep them separated. Can some one suggest some link or better shows the own experience ?



    Thanks in advace

  • #2
    Re: best practice for code separation

    Personally, I rarely output HTML from a program these days. I'm typically outputting JSON (or, less frequently, XML) data that's interpreted by a client-side framework.

    Comment


    • #3
      Re: best practice for code separation

      Hi Scott

      many thanks for you reply/suggestion. As i'm quite new to PHP and web application, can you suggest me some link/tutorial that introduce you tecnique "outputting JSON (or, less frequently, XML) data that's interpreted by a client-side framework"

      Thanks in advance

      Comment


      • #4
        Re: best practice for code separation

        Err... which framework are you using? They're all different. And, I'm not familiar with all of them!

        Seems like this is sufficiently simple that once you know the framework, outputting JSON or XML is pretty trivial?

        Comment


        • #5
          Re: best practice for code separation

          Hi
          as i said this are my fisrst steps in web application; so are all new arguments. For the framework i though to use jQuery (it seems the most popular !!??) .
          If you or other can susggest me some link/tutorial for try simple example i would be gratefull.

          Thanks in advance

          Comment


          • #6
            Re: best practice for code separation

            The simplest way to separate them for a basic page is to keep the HTML in one file and the PHP in another. This will give you the basic idea (it has _not_ been tested):

            Script.php
            PHP Code:
            <?php
            $a 
            "My Name";
            $b 123.45;

            include 
            MyHtml.html
            ?>
            MyHtml.html
            HTML Code:
            <html><body> etc.
            
            <p>This html text is being enhanced by a php script. For example my name is: <?php echo $a; ?> and I earn the magnificent sum of $<php echo $b; ?> per week.</p>
            
            etc. etc. 
            Most html editors understand that HTML may contain PHP code islands and the WYSIWYG editors will normally mark the PHP section with a descriptive blob of some kind.

            To go to the next step usually involves either building the entire page in HTML/Javascript (using a Javascript framework such as Dojo, JQuery, etc.) and have the page make Ajax requests for the data to populate the page. As a result the PHP has little or no HTML content because it is just supplying data.

            Alternatively (or in combination with that approach) you would use a Framework such as Zend Framework, or Cake, or Code Igniter, or ... which tend to be based on MVC principals with the inherent emphasis on separation of view etc.

            Another option for supplying a more sophisticated version of the original two file approach is to use one of the templating systems such as Smarty.

            Hope this helps.

            Comment


            • #7
              Re: best practice for code separation

              The way that I've typically done what you want to do is the following....

              1) You have your view as HTML/CSS/Javascript
              2) Any server side processing that needs done such as data fetching from the database and json serialization can be done with PHP. Keep in mind that javascript can only call a PHP file which would be a PHP function. Javascript cannot call PHP classes directly. So if you are trying to use OO PHP features you can, but they will be one call level under your interfacing functions (not really a bad thing).
              3) Use a JS framework... my favorite is JQuery. But others are very good as well. JQuery supports JSON processing very well.
              4) You can use PHP's json_encode and json_decode to create and parse to/from PHP datastructures.

              This is my typical setup. Your mileage may vary.
              Your future President
              Bryce

              ---------------------------------------------
              http://www.bravobryce.com

              Comment

              Working...
              X