Author Topic: Separating data from layout without a template  (Read 751 times)

Offline marcuslee

  • Level 3
  • *
  • Posts: 6
  • Reputation: +0/-0
    • View Profile
Separating data from layout without a template
« on: April 15, 2010, 09:34:50 AM »
This is sort of a continuation from the following thread:

http://community.bbgamezone.net/index.php/topic,1351.0.html

As I was reading through that thread, I was thinking, "Y'all are working really hard, trying to keep your data separate from your layout."

If you place all of your PHP data calculations at the top of the file and your html under that, wouldn't you skip the double file confusion?

I understand that if your project is a huge one, that you may not want your web designers to even have access to your PHP code. But, for a small group, wouldn't a good web designer be able to scroll past the PHP?

In fact, several lines of spaces or lines commented out should be able to make it easy to see the difference.

And, if you are wanting to be able to change out designs/layouts later on, wouldn't you want to have seperate header, footer, etc ... files anyway?


Mark  8)

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: Separating data from layout without a template
« Reply #1 on: April 15, 2010, 10:55:19 AM »
Well basicly that's all the magic imho....  of course you want to then output the data from PHP into HTML, then you either use templating system or just basic PHP commands .... difficulty to learn is the same, PHP rather always much powerful and faster, you can screw template syntax as well as PHP's.... the only think I can think of now is that you can screw something else if you start - for some really weird reason - writing code into html you're not supposed to (like changing database data instead of just echoing), I'm don't know how to restrict this in PHP
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline marcuslee

  • Level 3
  • *
  • Posts: 6
  • Reputation: +0/-0
    • View Profile
Re: Separating data from layout without a template
« Reply #2 on: April 16, 2010, 12:00:44 AM »
I think I understand what you are trying to say. Not sure. It's late!  :-\

Can't really tell if you agree with me or not. Sorry.

Here's kind of what I had in mind:

Code: [Select]
<?php
//
// ---- A block of php code at the top of the page
//
$hello "Hello World!";
//
//
?>


<html>

<!-- Then, your HTML section -->

<!-- If you need to refer to a variable set at the top, use the following syntax -->

Welcome Message: <?php $hello ?>

</html>

I don't see why a template system couldn't be all in one file like this. Besides, a good web designer should know some elementary PHP ... if not being an expert in such things.  Even if all you know is HTML and CSS, you should be able to recognize PHP and either leave it alone or transcribe it correctly.

But, hey, that's just my two cents.  :P

Mark

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: Separating data from layout without a template
« Reply #3 on: April 16, 2010, 12:42:45 AM »
Yes, I agree....not sure it can be called 'template system' this way, but other than that I agree and that's basicly the way I'm going to do this
I already tried Smarty and really it's the same, but you have to copy every variable you want to use in template and then use syntax almost the same but less powerful as PHP...
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline dsheroh

  • Level 21
  • *
  • Posts: 235
  • Reputation: +6/-0
  • Perl Vicar
    • View Profile
    • Psi Rangers
Re: Separating data from layout without a template
« Reply #4 on: April 16, 2010, 05:33:32 AM »
I suppose it might be a bit of work to learn how to use templates properly and to get used to doing it that way, but the amount of work saved - and the amount of flexibility gained - by cleanly separating content from presentation is huge.

Everything so far has been talking about "presentation" solely in terms of "the page used to display stuff in a browser", but it goes well beyond that.  If they are fully separated, then you're not even limited to just changing page layouts, you can quickly and easily create command-line clients (good for quick-and-dirty testing without worrying about interface details), automated testing suites, or even desktop GUI apps which all tap into exactly the same back-end code, so you know they'll all behave similarly.

Real-life example:  I've been overhauling my game's combat system lately.  Last week, I'd gotten to the point of feeling pretty good about it in my command-line testing environment, so it was time to expose it through the web interface by adding a "Tactical" screen.  Here's the (Perl, not PHP) code that was required to do so:
Code: [Select]
package PRangers::CGI::Tactical;

use strict;
use warnings;

sub register {
  my (undef, $hooks) = @_;

  $hooks->{actions}->{tactical} = \&tactical_display;
}

sub tactical_display {
  my $r = shift;

  return 'guest.tt' unless $r->session->user;

  $r->tpl_data->{char} = $r->session->char;
 
  $r->conf->pagetitle('Tactical Display');
  return 'tactical.tt';
}

1;

I also needed to create the template ('tactical.tt'), of course, which I did by taking the main character profile template and replacing the cargo manifest with a battle log (Template::Toolkit code):
Code: [Select]
<h3>Combat Log</h3>
<p><i>[% char.battle_name || 'No log available' %]</i></p>

<div id=combat_log>
[% FOREACH round IN char.battle_log.keys.sort.reverse -%]
<div class=combat_round>
<p class=combat_timestamp>[% round | time_only %]</p>
[% FOREACH message IN char.battle_log.item(round) -%]
<p>[% message | bbcode %]</p>
[% END  # FOREACH message -%]
</div>
[% END  # FOREACH round -%]
</div>

That's all.  Because all the processing was already completely isolated from its interface, all it took was those few lines of code and about 10 minutes (if that) and I had a completely new /tactical URI showing players their characters' latest combat results.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal