question on creating template separated from the coding

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

question on creating template separated from the coding

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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..
User avatar
Sema
Forum Commoner
Posts: 34
Joined: Fri Sep 03, 2004 12:43 pm
Location: Aalborg, Denmark

Post 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=&quote;loop_count&quote;}
   &lt;td&gt;{$loop_array&#1111;loop_count]}&lt;/td&gt;
{/section}
A template engine can realy help you to seperate code and display, i hope this will help ;D
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

That's exactly what smarty does - But beware, smarty slows the load time of your site considerably.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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? :)
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

thank you all :)
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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 :wink: Maybe my results were not normal, who knows.
User avatar
Sema
Forum Commoner
Posts: 34
Joined: Fri Sep 03, 2004 12:43 pm
Location: Aalborg, Denmark

Post 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.
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post 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.
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post 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 :D

Sphen001
Post Reply