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: - <?php
-
function makeContent() { /* … */ }
-
// some more code
-
-
/* printing out the HTML code */
-
include "header.htm";
-
makeContent();
-
?>
the template way: - <html>
-
<head>…</head>
-
<body>
-
<?php
-
makeContent();
-
?>
-
</body>
-
</html>
the way that currently doesn't come to my mind:
regards
15 2429
The totally abstracted way (more like template way). -
[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.
but how do you put that into code/markup?
@Dormilich
I don't, hehe. I let CodeIgniter do it for me. It's out of my know-how, tbqh.
well, at some point you have to tell the parser, what to do actually. (ok, I didn't dive into the CodeIgniter system....)
But, if I were to elaborate, and give you an example: -
//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.
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.
@Dormilich
Sure, but it's not too much PHP, just the basic looping through arrays, echoing variables, etc.
I kind of do a mixture of the two. Like so: - <?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
to put in my style: - <?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>
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.
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.
the book that helped me a lot is "PHP 5 Power Programming".
@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)
@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
@both
I get the impression that most of us learn it this way.....
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |