开发者

How to implement template logic in PHP

Having my first attempt with PHP templates and i'm a bit confused separating business with presentation logic. I've choosen Dwoo as my template engine as you will see on the example below but my question is independent of Dwoo. I see that what im doing isn't a good logic, it'll confuse the designer and it doesn't really separates PHP/HTML. I can't think any other way of doing it so please if anyone have the time explain me a bit.

Cheers.

PHP Code

$trans = translation('LABEL');

// Load Template Engine

$dwoo = new Dwoo(); $dwooTpl = new Dwoo_Template_File('tpl/label.tpl.html'); $dwooData = new Dwoo_Data(); $dwooData->assign('title','Page Title');

if(!isset($_SESSION['logged'])){ $dwooData->assign('logged',0); $dwoo->output($dwooTpl,$dwooData); }else{ $userID = $_SESSION['userID']; if (!isset($_POST['add'])){ $sql = "SELECT labelName FROM label WHERE userID = $userID"; $rs = mysql_query($sql) or die('Can\'t get label'. mysql_error()); if(mysql_num_rows($rs)){ while ($row = mysql_fetch_object($rs)){ $labels[] = $row->labelName; } $dwooData->assign('labels',$labels); }

    $dwooData->assign('logged',1);
    $dwooData->assign('addLabel',0);
    $dwoo->output($dwooTpl,$dwooData);

}else{

    $labelName = mysql_real_escape_string($_POST['labelName']);
    $labelName = trim($labelName);
        if(strlen($labelName) < 1)
        {
            $dwooData->assign('ERR1','Label Name is not valid.
                  You must enter at least 1 letter as Label开发者_如何学Go name.');
        }else{
            $sql = "SELECT labelName
                    FROM label
                    WHERE userID = '$userID'
                    AND labelName = '$labelName'";
            $rs = mysql_query($sql) or die('Can\'t get label '. mysql_error());
            if(mysql_num_rows($rs)){
               $dwooData->assign('ERR2',"Label '<strong>$labelName</strong>' already exists.");

            }else{
                $sql = "INSERT INTO label
                        VALUES('','$userID','$labelName')";
                mysql_query($sql) or die('Can\'t insert Label '. mysql_error());
                $dwooData->assign('INFO','Label added Succesfully.');
             }
        }


$dwooData->assign('logged',1);
$dwooData->assign('addLabel',1);
$dwoo->output($dwooTpl,$dwooData);

}

}

Template Code

{include(file='header.tpl.html')} {if $logged == 1} {if $addLabel == 1} {if $ERR1} {$ERR1} {else} {if $ERR2} {$ERR2} {else} {$INFO} {/if} {/if} {else} Add Label: <form method="POST" action="label.php"> Name: <input type="text" name="labelName"><br> <input type="submit" value="add" name="addLabel"> </form> {foreach $labels label} {escape($label)} {/foreach} {/if} {else} You must login to access this page. {/if} {include(file='footer.tpl.html')}


In this case, turn it into three distinct template files:

  • a single $ERRMSG template
  • one "You must login.." page
  • and a form

The application logic should define which of the three cases gets displayed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜