LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   PHP Templating (http://www.linuxquestions.org/questions/programming-9/php-templating-839000/)

delite 10-19-2010 04:39 AM

PHP Templating
 
Hi, i'm working on a small CMS project. When starting out I didn't know too much CSS, so virtually everything was wrapped in HTML and a little CSS. The main page templates used the following functions:

Code:

function theme_stylise($data,$tpl='node')
{
        global $theme_blocks;
        return theme_evaluate($theme_blocks[$tpl],$data);
}

function theme_evaluate($s,$data=array())
{
        global $page;
        ob_start();
        $x=@eval("?>".$s);
        $l=ob_get_contents();
        ob_end_clean();
        return $l;
}


// EXAMPLE TEMPLATES

$theme_blocks['login_user']="<div class='login_user'><?php print \$data['content']; ?></div>";

$theme_blocks['profile_menu']="<div class='profile_menu'><?php print ' | '; foreach(\$data['menu'] as \$e){ print \$e.' | ';} ?></div>";


Further down the line I also added the following:

Code:

list($tmp_list,$tmp_node)=shop_templates('delivery');
$_SHIPPING_.=replace_all($tmp_list,array($_SHIPPING_ITEMS_,$_SHIPPING_DEST_,$_SHIPPING_TITLE_),$tmp_node);


// EXAMPLE TEMPLATES

function shop_templates($ref,$type=0)
{
        if($ref=="invoice"){
                $s="
                <h2>Order Reference:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ORDER_REF_</h2><br />
                <h2>Order Details</h2><table><tr class='tr_pch'><td><b>Status</b></td><td><b>Order Ref</b></td><td><b>Date</b></td><td><b>Total Cost</b></td></tr>
                <tr><td colspan='99'><hr /></td></tr>
                <tr class='tr_pc1'><td>_ORDER_STATUS__ORDER_RESEND_</td><td>_ORDER_REF_</td><td>_ORDER_DATE_</td><td>_ORDER_TOTAL_</td></tr>
                <tr><td colspan='99'><hr /></td></tr>
                </table>
                <br /><br />
               
                <h2>Order Items</h2>
                <table>
                <tr class='tr_pch'><td><b>Status</b></td><td><b>Title</b></td><td><b>Ref</b></td><td><b>Qty.</b></td><td><b>Items Cost</b></td><td><b>Shipping</b></td><td><b>Total</b></td></tr>
                <tr><td colspan='99'><hr /></td></tr>
                _INVOICE_ITEMS_
                <!--<tr><td colspan='99'><br /><hr /><br /></td></tr>-->
                </table><br />
                ";
               
                return array(array("_ORDER_STATUS_","_ORDER_RESEND_","_ORDER_REF_","_ORDER_DATE_","_ORDER_TOTAL_","_INVOICE_ITEMS_"),$s);
        }
        elseif($ref=="invoice_item"){
                $s="<tr class='_ODD_'><td>_STATUS_</td><td>_ITEM_TITLE_</td><td>_ITEM_REF_</td><td>_QTY_</td><td>_ITEM_COST_</td><td>_SHIP_TITLE_ _SHIP_COST_</td><td>_TOTAL_</td></tr>\n";
                $s.="<tr class='_ODD_'><td colspan='7'>_DROPBOX_</td></tr>\n";
                $s.="<tr><td colspan='7'><hr /></td></tr>\n";
                return array(array("_ODD_","_STATUS_","_ITEM_TITLE_","_ITEM_REF_","_QTY_","_ITEM_COST_","_SHIP_TITLE_","_SHIP_COST_","_TOTAL_","_DROPBOX_"),$s);
        }
        elseif($ref=="size_item"){
                $s="<div class='shop_size_item'>_SIZE_RADIO_ _SIZE_TITLE_ : _SIZE_COST_ - _SIZE_DESC_</div>";
                return array(array("_SIZE_RADIO_","_SIZE_TITLE_","_SIZE_DESC_","_SIZE_COST_"),$s);
        }
        return null;
}


Now most small page parts use the second method, and are then grouped together by the first method.

After implementing the second method in a single module I realised that it was being called quite a bit.

So before updating the rest of the CMS with this method I'd like to hear your views on efficiency, templating, either using either of the above methods, or any more you may suggest.


All times are GMT -5. The time now is 05:13 AM.