473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

content – code separation

Dormilich
8,658 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function makeContent() { /* … */ }
  3. // some more code
  4.  
  5. /* printing out the HTML code */
  6. include "header.htm";
  7. makeContent();
  8. ?>
the template way:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>…</head>
  3.     <body>
  4. <?php 
  5.   makeContent();
  6. ?>
  7.     </body>
  8. </html>
the way that currently doesn't come to my mind:
Expand|Select|Wrap|Line Numbers
regards
Mar 11 '09 #1
15 2429
Markus
6,050 Expert 4TB
The totally abstracted way (more like template way).

Expand|Select|Wrap|Line Numbers
  1. [controller]
  2.         -> request data from model 
  3.                 [model]
  4.         <- model return data to controller
  5. [controller]
  6.         -> create array of returned data
  7.         -> load view, and array of data
  8.                 [view]
  9.                         -> use loaded variables
  10.  
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.
Mar 11 '09 #2
Dormilich
8,658 Expert Mod 8TB
but how do you put that into code/markup?
Mar 11 '09 #3
Markus
6,050 Expert 4TB
@Dormilich
I don't, hehe. I let CodeIgniter do it for me. It's out of my know-how, tbqh.
Mar 11 '09 #4
Dormilich
8,658 Expert Mod 8TB
well, at some point you have to tell the parser, what to do actually. (ok, I didn't dive into the CodeIgniter system....)
Mar 11 '09 #5
Markus
6,050 Expert 4TB
But, if I were to elaborate, and give you an example:


Expand|Select|Wrap|Line Numbers
  1. //Class 'example.php' :
  2. // Controller is the super object - all our controllers must extend it.
  3. class Example extends Controller 
  4. {
  5.     public function Example( )
  6.     {
  7.         // init the parent's constructor.
  8.         parent::Controller();
  9.         // Load my model, with alias.
  10.         $this->load->model( 'Pm_model', 'MailModel' );
  11.     }
  12.     public function inbox( )
  13.     {
  14.         // Grab some data from the model, assign it to a variable (in array).
  15.         // Maybe use a where clause
  16.         $this->db->where( 'pm_to', $this->session->userdata( 'username' ) );
  17.         $vars['mail_meta'] = $this->MailModel->grab_mail_meta( );
  18.         // Load the view, second param: variables for use in view.
  19.         $this->load->view( 'example', $vars );
  20.     }
  21. }
  22.  
  23. // Model: Pm_model.php
  24. class Pm_model extends Model
  25. {
  26.     // call parent constructor, etc.
  27.  
  28.     public function grab_mail_meta( )
  29.     {
  30.         // grab the data.
  31.         // return array, object, etc.
  32.         return $data_array;
  33.     }
  34. }
  35.  
  36. // View: example.php
  37. <table>
  38.   <?php foreach ( $mail_meta as $row ) : ?>
  39.   <tr>
  40.     <td><?php echo $row['id']; ?></td>
  41.   </tr>
  42.   <?php endforeach; ?>
  43. </table>
  44.  
  45.  
That's a barebones example.

I do suggest CodeIgniter as a framework. It's wonderful.
Mar 11 '09 #6
Dormilich
8,658 Expert Mod 8TB
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.
Mar 11 '09 #7
Markus
6,050 Expert 4TB
@Dormilich
Sure, but it's not too much PHP, just the basic looping through arrays, echoing variables, etc.
Mar 11 '09 #8
TheServant
1,168 Expert 1GB
I kind of do a mixture of the two. Like so:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $var1 = $_GET['something'] * 10;
  3. include('other_vars.php');
  4. $asd = 23*$var32;
  5. ...
  6. ?>
  7. <body>
  8. <div>
  9. <p>This is a number: <?php echo($asd); ?></p>
  10. </div>
  11. </body>
  12.  
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
Mar 11 '09 #9
Dormilich
8,658 Expert Mod 8TB
to put in my style:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // script loader
  3.     require_once 'load.main.php';
  4.  
  5. // try output class
  6. try {
  7.     $kbl = new HTMLplus("main");
  8. }
  9. catch (Exception $me)
  10. {
  11.     trigger_error($me->getMessage() . $me->getTraceAsString(), E_USER_ERROR);
  12. }
  13. // start content output/layout
  14.     $kbl->Head();
  15.     $kbl->Titel();
  16.     $kbl->Meta();
  17.     $kbl->CSS();
  18.     $kbl->JS();
  19. ?>
  20.     </head>
  21.     <body>
  22. <!-- some static HTML -->
  23. <?php
  24.     $kbl->Navi();
  25.     $kbl->Inhalt(); // ⇐ that's the content
  26. ?>
  27. <!--  more static HTML  -->
  28. <?php
  29. // include FF and W3C banner
  30.     $kbl->include("banner");
  31. ?>
  32.     </body>
  33. </html>    
Mar 11 '09 #10
Markus
6,050 Expert 4TB
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.
Mar 11 '09 #11
TheServant
1,168 Expert 1GB
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.
Mar 11 '09 #12
Dormilich
8,658 Expert Mod 8TB
the book that helped me a lot is "PHP 5 Power Programming".
Mar 11 '09 #13
Dormilich
8,658 Expert Mod 8TB
@Markus
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)
Mar 11 '09 #14
Markus
6,050 Expert 4TB
@TheServant
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
Mar 11 '09 #15
Dormilich
8,658 Expert Mod 8TB
@both

I get the impression that most of us learn it this way.....
Mar 11 '09 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Eric Veltman | last post by:
Hello everyone, I'm getting somewhat annoyed about how many cms's there are in PHP/MySQL while so few are really good. Yesterday I started to like ezPublish, until I decided the template...
12
by: jonathan.beckett | last post by:
Hi All, For the past few months I have been working on an open source Apache/PHP/MySQL content management system - and have recently made it available for download. It's still very much a...
0
by: jonathan.beckett | last post by:
Hi All, I have just made version 0.4.8 of the PluggedOut CMS Content Management System available for download - it's free, and covered by the GPL. It's still very much a work in progress...
20
by: Griff | last post by:
Hi there I'm after some suggestions as to how one might best separate form and content in a normal run-of-the-mill web application. I'm sure whole bookshelves have been written on this, but I...
25
by: Alvin Bruney | last post by:
C# is great but it does have some short comings. Here, I examine one of them which I definitely think is a shortcoming. Coming from C++, there seems to be no equivalent in C# to separate code...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
8
by: neilmcguigan | last post by:
I just wanted to list some reasons why I prefer inline code to code-behind. 1. you can fix some bugs more quickly. remote desktop into server, change the aspx file, and she's good to go. I'd say...
2
by: Mr Flibble | last post by:
I have data in XML. I need to be able to present the data in a web page which may be charts, tables and other such like controls. I need to be able to modify the appearance of this page simply...
0
by: dbpokorny | last post by:
On Jun 27, 9:09 am, "John Salerno" <johnj...@NOSPAMgmail.comwrote: This is a little anecdote about "separation of presentation, content, and logic." I used to work in a web application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.