pea Posted November 28, 2008 Share Posted November 28, 2008 I'm looking to create a templating system for one of my projects but am having trouble understanding the concept. What i've gathered so far is that a basic templating system would look something like this: index.php <?php $word = "something"; include("template.php"); ?> template.php <b><?=$word;?></b> But i'm confused at how you might do more complex things. For example, a forum table. You need to generate the html table in a loop, but also template that html. How are templates managed in this case? Link to comment https://forums.phpfreaks.com/topic/134629-templating-concepts/ Share on other sites More sharing options...
Adam Posted November 28, 2008 Share Posted November 28, 2008 I'd recommend using SMARTY: http://www.smarty.net/ A snippet taken out of a file I've recently being using at work. Perhaps quite confusing from an outside point of view but just shows how well SMARTY can handle everything.. <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> {* Loop through the items on the left hand side (if any) *} {if !empty($items.left)} <td valign="top" {if !empty($width)}width="{$width}%"{elseif !empty($items.left.0.style)} style="{$items.left.0.style}"{/if}> {foreach from=$items.left item=item} <div {if !empty($item.style)}style="{$item.style}" {/if}class="text">{$item.html}</div> {/foreach} </td> {/if} {* Loop through the items in the centre (if any) *} {if !empty($items.centre)} <td valign="top" {if !empty($width)}width="{$width}%"{elseif !empty($items.centre.0.style)} style="{$items.centre.0.style}"{else} style="width: {$width}%; padding-left: 10px; padding-right: 10px;"{/if}> {foreach from=$items.centre item=item} <div {if !empty($item.style)}style="{$item.style}" {/if}class="text">{$item.html}</div> {/foreach} </td> {elseif !empty($items.content)} <td valign="top" {if !empty($width)}width="{$width}%"{elseif !empty($items.content.0.style)} style="{$items.content.0.style}"{else} style="width: {$width}%; padding-left: 10px; padding-right: 10px;"{/if}> {foreach from=$items.content item=item} <div {if !empty($item.style)}style="{$item.style}" {/if}class="text">{$item.html}</div> {/foreach} </td> {elseif $items.form.0.type == 'HTML'} <td valign="top" {if !empty($width)}width="{$width}%"{elseif !empty($items.content.0.style)} style="{$items.content.0.style}"{else} style="width: {$width}%; padding-left: 10px; padding-right: 10px;"{/if}> <div {if !empty($items.form.0.style)}style="{$items.form.0.style}"{/if}>{$items.form.0.html}</div> </td> {elseif $items.form.0.type == 'Form Settings'} <td valign="top"> {include file="elements/form.tpl" form=$items.form} <br /> </td> {/if} {* Loop through the items on the right hand side (if any) *} {if !empty($items.right)} <td valign="top" {if !empty($width)}width="{$width}%"{elseif !empty($items.right.0.style)} style="{$items.right.0.style}"{/if}> {foreach from=$items.right item=item} <div {if !empty($item.style)}style="{$item.style}" {/if}class="text">{$item.html}</div> {/foreach} </td> {/if} </tr> </table> Pretty simple to setup and very powerful! Adam Link to comment https://forums.phpfreaks.com/topic/134629-templating-concepts/#findComment-700949 Share on other sites More sharing options...
GKWelding Posted November 28, 2008 Share Posted November 28, 2008 If, for some strange reason you don't just want to use Smarty and want to create your own, check out this for some good guidance... http://www.broculos.net/tutorials/how_to_make_a_simple_html_template_engine_in_php/20080315/en Link to comment https://forums.phpfreaks.com/topic/134629-templating-concepts/#findComment-700985 Share on other sites More sharing options...
pea Posted November 29, 2008 Author Share Posted November 29, 2008 Maybe Smarty is the way to go if the syntax is known by templaters, but if not i don't want to give them anything extra to learn. I don't think the regex way accounts for loops. Link to comment https://forums.phpfreaks.com/topic/134629-templating-concepts/#findComment-701627 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.