Help - Search - Members - Calendar
Full Version: Separation of logic from presentation
404 Daily: File Found > Popular Culture > Technical Discussion
Mormegil
QUOTE
yeah, 3 hours 'away.' just let me say that i don't appreciate the fact that php is embedded in existing html documents. I kinda like the seperation between .aspx pages and the underlying code. slipping code between embedded tags just don't do it for me, i gotta say. i'll learn to cope, but that's just odd practice, imo.

html is a markup language, php is something completely different. i'm gonna miss that segregation beteen the two


Another three hours later, heh. I've never used ASP before, so I've no idea how it works. If it's anything like Perl/CGI, which is what I started out with, where you generate HTML using the language's equivalent of the print function, then I really do appreciate PHP's embedded nature. The separation of presentation from logic is very important, I agree. With PHP, you've got the flexibility to choose to what degree you perform this separation. The ability to easily insert small snippets of code inline is a godsend. In my own code, I keep 99% of the programming separate from the markup and insert it where I need to using includes or classes.

QUOTE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 STRICT//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml11-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="<?php echo $site['lang'] ?>">
<head>
<title><?php echo $title ?></title>
</head>
<body>
</body>
</html>


The variables embedded in this example might be set by a function or class that either generates or includes the surrounding HTML code. For example, a function to display the pages heading might be similar to the following.

CODE
function print_heading($lang,$title) {
     include 'page_heading.php';
}


This is a pretty crappy example, but I hope it demonstrates at least to some degree how it's perfectly possible to separate presentation from logic. I'm going to cut myself some slack either way since I've just woken up from a grand four hours of sleep.

Edit: As far as maintainability goes, here's a more concrete example of how easy it is to update an entire site with PHP just by editing a line or two in an include file.

CODE
Logic (includes.php):

function print_menu {
     $menu = array ( 'Home' => 'home',
                  'Titties' => 'titties',
                  'Gadgets' => 'gadgets',
                  'Contact Us' => 'contact',
                );
                              
     foreach ( $menu as $page => $ident ) {
          $menu .= "<li><a href=\"index.php?act=$ident\">$page</a></li>\n";
     }

     return $menu;
}

/* ---------------------------------------------------------------------------------------------------------- */

Presentation (index.php):

<?php include includes.php ?>

<html>
<head>
</head>
<body>
<ul id="mainnav">
<?php print_menu() ?>
</ul>
----- CONTENT HERE ------
</body>
</html>


Here you see the ability for a site to update it's menu simply by modifying the array in the file includes.php. This method allows you to be flexible in the maintenance of your site while leaving the styling of the menu completely separate from the PHP coding. An alternative would be to allow you to pass the menu items to the print_menu function inside of the HTML code.

CODE
In the HTML:

<body>
<?php print_menu('Home' => 'home', 'Titties' => 'titties...etc) ?>
</body>

In the PHP:

function print_menu($links) {
     // Function is identical, except that the array is already defined by the $links argument
}
Brent Black
Oh god I love you, Morm. I certainly wasn't expecting a reply like this.

In ASP, you can embed code in the same way you do with PHP, and it's parsed out by the server all the same, but it always seemed like an oil and water situation to me. The other way ASP works is with an actual code page behind the .aspx 'face.'

Example:

hello.aspx
CODE
<html>
<head>
  <title>Hello World</title>
</head>
<body>
  <asp:label id="Message1" forecolor="red"  text="Hello" runat="server" />
</body>
</html>


Now if you want to change that label when the page loads...

hello.aspx.vb
CODE
Partial Class hello
  Inherits System.Web.UI.Page

  Protected Sub Page_Load (ByVal sender As Object, e As EventArgs)
    Me.Message1.Text = "And now for something completely different."
  End Sub
End Class


I can do the same thing with any object on the page that has been defined through ASP. The aspx.vb (or aspx.c if that's your language of choice) is completely separated from the html. Like I said, I could embed that subroutine right into the aspx page and it would be parsed out all the same, but the line between presentation and logic is more clearly drawn with the separate code page.

I like the way you do things. I guess I didn't get far enough into learning this yet to see that you can still keep your PHP in a separate file and include it in your html. I feel much better already.
Zero
It's worth noting that Clearsilver separates logic from presentation in much the same way as ASP, except less stupidly and independent of language.

I also started work on an offline templating system a while back (before seeing Clearsilver) that behaves kind of like that. You define a template for presentation that makes references to variables for things that vary from page to page. Then you write each page as a Lua script that defines these variables. Using something like GNU make, you smush the pages and templates together, and the resulting HTML output gives you your site. I've got some code up, but it doesn't make much sense as I'm pretty sure I wrote it in a half-hour on my 40th hour of consciousness a while back.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.