Reputation:
I'm making a simple template system. It's annoying to not to be able to keep the code in the PHP file and output it in html file... Its not nice to have this in the template:
<?php
while ($row = mysql_fetch_array($query)) {
$name = $row['forum_name'];
$date = $row['forum_date'];
$desc = $row['forum_description'];
$lastpost = $row['forum_lastpost'];
?>
<h1><?php echo $name ?></h1>
<p><?php echo $desc ?></p>
<?php } ?>
Is there some way I could keep the code in the PHP file?
Thanks
Upvotes: 2
Views: 1892
Reputation: 165
I like to use PHP as the templating engine and use an MVC pattern. In order to keep the View and Business logic separate only a few types of PHP code are allowed in the View, which is HTML code. The allowed code is:
* Single functions
* Alternate format If / Else / ElseIf blocks
* Alternate format For loops
* Alternate format Foreach loops
* Alternate format Switch statements
PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively. These alternate formats are the only formats that should be used in the View.
I will concede that the alternate PHP syntax is slower (because the interpreter is jumping in and out of PHP tags). But, this generally equates to milliseconds of processing time and makes little difference with today's servers in most environments.
Finally, I prefer to use shorthand PHP tags in the View. This is generally acknowledged as a bad idea because there is less server support. But, I believe it improves readability slightly (specifically when using PHP as a templating engine) and I would generally avoid a web host where I couldn't control such things.
I describe this in a tiny bit more detail and have a few examples on my web pages about a template I created for web based PHP apps. You'll find that at the URL below.
http://www.joeldare.com/wiki/applate
Upvotes: 1
Reputation: 74588
I would suggest doing the query and processing in the php file, not in the template. Build an array in the php file, then let the template display it.
This is how I typically do it:
PHP file:
for ($i = 0; $row = mysql_fetch_array($query); $i++)
{
$forums[$i]['name'] = $row['forum_name'];
$forums[$i]['date'] = $row['forum_name'];
$forums[$i]['desc'] = $row['forum_description'];
$forums[$i]['lastpost'] = $row['forum_lastpost'];
}
Template file:
<?php foreach ($forums as $forum) { ?>
<h1><?=$forum['name']?></h1>
<p><?=$forum['desc']?></p>
<?php } // foreach ($forums as $forum) ?>
An alternative syntax is using "endwhile" as glavić showed:
<?php foreach ($forums as $forum): ?>
<h1><?=$forum['name']?></h1>
<p><?=$forum['desc']?></p>
<?php endforeach; ?>
Upvotes: 3
Reputation: 43572
You can use template engine like http://dwoo.org/
Or use alternate syntax like:
<?php while ($row = mysql_fetch_array($query)): ?>
<h1><?=$row['forum_name']?></h1>
<p><?=$row['forum_description']?></p>
<?php endwhile; ?>
Upvotes: 0