How can we separate Logic from presentation without using any template engine (traditional php-not OOP) Thank in advance
+4
A:
PHP itself can be used as a template engine. Just put all your logic before you output anything. Put simply:
- Process your input
- Assign all the dynamic data to be output to variables
- Run your view code. The view code may be in a separate file which you include.
- In the view, just use things such as
echoandforeachto output the data you put into variables in step 2.
Matti Virkkunen
2010-04-04 13:02:44
+3
A:
Why not to use PHP itself as a template engine? It being used in the code I posted for your other question. Your program has to be split into 2 main sections: getting data and displaying data.
Each page have to have it's own template. In the code I posted there is 2 very simple templates, form.php and list.php
just extend it with the whole site template, And you have done!
Here is a little more complex PHP template example:
<table border="0" cellpadding="2" cellspacing="0" width="600">
<? foreach ($data as $row): ?>
<tr bgcolor="#666699">
<td align=left>
<font color="white"><b><?=$row['name']?></b></font>
</td>
<td align=right><font color="white">
<?=$row['date'] ?>
</font></td>
</tr>
<tr bgcolor="f0f0f0">
<td colspan=2><?=$row['body'] ?></td>
</tr>
<? if ($row['answer']): ?>
<tr bgcolor="d3d3d3">
<td colspan=2 valign="top">
<table border="0" cellpadding="0" cellspacing="5">
<tr>
<td valign="top"><b>Answer: </b></td>
<td><?=$row['answer'] ?></td>
</tr>
</table>
</td>
</tr>
<? endif ?>
<? if($admin): ?>
<tr>
<td colspan=2>
<font size=-1>
<?=$row['id']?> - <?=$row['ip']?> - <?=$row['topic']?>
<? if($row['del']): ?>
<a href="/gb/?action=show&id=<?=$row['id']?>">show</a>
<? else: ?>
<a href="/gb/?action=hide&id=<?=$row['id']?>">hide</a>
<? endif ?>
<a href="/gb/?action=edit&id=<?=$row['id']?>">edit</a>
</font>
</td>
</tr>
<? endif ?>
<? endforeach ?>
</table>
And it is called like this
<?
//some code to get data
include 'tpl_top.php';
include 'tpl_list.php';
include 'tpl_bottom.php';
?>
Looks magnificent to me!
Dunno though, if it's what you're asking for :)
Col. Shrapnel
2010-04-04 14:54:29
I would use brackets for the if/foreach statements (so the IDE can find them and highlight them) also, you should probably use full tags just in case the target environment doesn't have shortags enabled (though, you could preparse the code if you really wanted to similar to what code igniter does).
SeanJA
2010-04-04 14:58:33
@Sean I would not, as it's more HTML than PHP. And I have much more requirements for my code to run. So, short tags isn't a problem
Col. Shrapnel
2010-04-04 15:01:18
Why are we using tables for layout here? ... ... ... *chirp* ... ...
Moshe
2010-04-04 15:09:31
Dear @Moshe You are free to use anything you want, even XML :)
Col. Shrapnel
2010-04-04 16:27:37
@Col. Shranpnel - Sure, you are. But tables based layout is considered bad practice nowadays. I guess for an example it's fine, but generally, you want to be using CSS.
Moshe
2010-04-04 16:44:21
@Moshe I would use anything that suits me and works well, if you please.
Col. Shrapnel
2010-04-04 17:03:32
@Col .Shrapnel - I'm not trying to offend you. I'm just saying that web standards today advocate separating content from layout using CSS. A relic of early 1990's web design was using tables to force everything to layout properly. This practice, among other things, wastes bandwidth and was therefore discarded along with `<font>` tags and whatnot in the early part of the 21st century. Google `Jeffrey Zeldman` if you want to learn more about standards web-design. Although, If you are displaying tabular data (charts and statistics, for example) then by all means, that is what tables are for.
Moshe
2010-04-04 18:08:06
using <table> tag for data or admin panel boxes is better than using div
2010-04-04 18:09:12
@jasmine - For data, yes. But for admin panel boxes?! No way. That is going back in time in web design. Not a chance. Again, layout is not not what `<table>` is for.
Moshe
2010-04-04 18:10:39
@moshe;Our issue is php not css :)@ Col. Shrapnel, thanks for answer. It what Im asking for..
2010-04-04 18:12:26
@jasmine - I'm just pointing it out. You were asking about separating logic from presentation which is exactly what CSS is for. i know that you were not asking on that level, but seeing this kind of code, about 15 years behind its time, I felt obliged to point out the bad practice. Granted, this is just an example, I an not trying to insult anyone If it gets the job done, as they say in yiddish, *gezundeheit*.
Moshe
2010-04-04 18:16:03
@Moshe you've got it too far. One can't separate logic from presentation using CSS. That's impossible. There are no loops or conditions in CSS. Variations of presentation - yes. Presentation itself - no. You can't add admin pane to each comment using CSS. that's it. Speaking of this layout, comments list is merely a table. I see nothing bad in using it this way.
Col. Shrapnel
2010-04-04 18:24:10
@Col Shrapnel - Okay. That's fine.
Moshe
2010-04-04 18:50:27
@jasmine: I don't think there's a policy that says that people may only discuss your question and your question exactly.
Matti Virkkunen
2010-04-04 19:55:33
@Matti Virkkunen ; You are right.
2010-04-04 20:08:53
@Col. Shrapnel: I don't know, I still find using if ... endif; to be a pain in the arse when I am trying to find the closing of an if statement. To each his own I guess.
SeanJA
2010-04-06 01:05:48
@SeanJA Are you using it in templates or in the PHP code? I am using only in templates, which has not too complex code.
Col. Shrapnel
2010-04-06 04:21:04
@Col. Shrapnel: I still find it hard in the templates to find the end tags, and it is too similar to Smarty *shudder*... anyway, I have opened a ticket for Netbeans about the highlighting, so hopefully they will fix it and all will be right with the world ;)
SeanJA
2010-04-07 03:51:47