Page 1 of 1
question on creating template separated from the coding
Posted: Sun Apr 17, 2005 12:13 am
by hongco
hello,
so far, most of my applications contain codings separated from html, but not just purely coding. I mix between 60-70 codes with 30-40 html together. I really want to do them separately.
How would you create a template where the coding will return some looping? I am not sure if you understand what i mean, let me give a simple sample
this phpbb forums..the main page will return a list of forum name, how would you use your template to display the list?
Thank you!
opps, sorry i double posts. the server was very slow at one point
Posted: Sun Apr 17, 2005 2:00 am
by infolock
dunno what you mean by looping..?
as far as exiting php to do your html, you could either do that or just echo it out.
either way, the only loop i could guess you mean is something like this :
Code: Select all
echo '<table><tr>';
for($i=0; $i<5; $i++)
{
echo '<td>this is cell '.$i.'</td>';
}
echo '</tr></table>';
or it could be written
Code: Select all
<?
// blah some php code above
?>
<table><tr>
<?
for($i=0; $i<5; $i++)
{
?>
<td>This is cell <? echo $i; ?></td>
<?
{
?>
of course, by the extra lenght in code for the second method, you can see why it's normally *better coding practice* to just echo the html out. simply due to it lessinging the amount of line spacage you use.
however, it really doesn't matter, but more or less whichever way you are most comfortable. I just hate moving in and out of php whereas if i keep it all inside php it is easier to just perform extra actions whenever i please without the need to reenter php..
Posted: Sun Apr 17, 2005 3:06 am
by Sema
If you want to seperate the code and display then use at template engine... with that you can send a array with a list form your PHP code to the template, that then loops it out in the display... In this case i am using the smarty template engine
http://smarty.php.net
PHP:
Code: Select all
<?php
//array that must be looped
$array = array('blue', 'red', 'green');
$template = new smarty();
$template->assign('loop_array', $array);
$template->display('template.tpl');
?>
Template:
Code: Select all
{section loop=$loop_array name="e;loop_count"e;}
<td>{$loop_arrayїloop_count]}</td>
{/section}
A template engine can realy help you to seperate code and display, i hope this will help ;D
Posted: Sun Apr 17, 2005 11:30 am
by hongco
hi,
sorry, what i meant is to use 100% html separated from coding. For instance, after you run a mysql query, the result will return few rows, and each row will have several outputs. All these rows will be printed out using table tags.
so, i wish to have one file with html only, something like this:
<table>
<tr>
<tq>{my name} {my phone} {my address}
</tq>
</tr>
<!-- more rows here -->
</table>
, the coding file should not contain any html tag.
Posted: Sun Apr 17, 2005 11:54 am
by Todd_Z
That's exactly what smarty does - But beware, smarty slows the load time of your site considerably.
Posted: Sun Apr 17, 2005 1:41 pm
by Roja
Todd_Z wrote:That's exactly what smarty does - But beware, smarty slows the load time of your site considerably.
Considerably is a suitably subjective word for such a subjective statement without context.
Rewritten slightly, I'd agree that "In some situations, depending on what your code does, on a server without a php accelerator, smarty will increase the processing time over the same process in pure php".
Of course, the same can be said of any *function* in php as well.. its all relative. Smarty has been used at Yahoo, and they are hardly a low-volume site producer. I've powered numerous web based games with it, and have similar results.
Any additional processing over "echo()" will add some processing time, so "considerably" is definitely a subjective term. Further, seperating display from control isn't generally about optimizing display time: Its about honoring a well-established programming best practice that helps improve maintainability.
Would you like to give some specific complaints and figures, so they can be debunked directly?

Posted: Sun Apr 17, 2005 6:16 pm
by hongco
thank you all

Posted: Mon Apr 18, 2005 12:13 am
by Todd_Z
On my site [
www.acdrifter.com], I experimented with smarty - Some pages went from .200 seconds to about 1 second - It was a
considerable increase in load time

Maybe my results were not normal, who knows.
Posted: Mon Apr 18, 2005 3:43 am
by Sema
Im dont know if it would help on your site, but if you dont have as much dynamic content, then have you tried to use the smarty cache funktion?... I havent used it much myself, but i think it can boost the speed a lot.
Posted: Mon Apr 18, 2005 11:34 am
by hongco
Sema wrote:Im dont know if it would help on your site, but if you dont have as much dynamic content, then have you tried to use the smarty cache funktion?... I havent used it much myself, but i think it can boost the speed a lot.
hi Sema,
Almost everything on my site is dynamic.
Posted: Mon Apr 18, 2005 4:58 pm
by Sphen001
One good thing to notice about template engines is that Smarty isn't the only one. It is probably the most well known, but there are others. As a result of Smarty's many abilities, it is larger, and therefore
somewhat slower than smaller, leaner template engines. If you are looking for simple variable replacement, perhaps you should search for a engine that does just that. I remember seeing a thread on this topic on another forum. It listed about 50 other engines. Try searching Google: PHP Template Engines.
I hope this helps
Sphen001