Jump to content

Templating Concepts


pea

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.