Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement template logic in PHP
    primarykey
    data
    text
    <p>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.</p> <p>Cheers.</p> <p>PHP Code <pre><code></p> <p>$trans = translation('LABEL'); </p> <p>// Load Template Engine<br> $dwoo = new Dwoo(); $dwooTpl = new Dwoo_Template_File('tpl/label.tpl.html'); $dwooData = new Dwoo_Data(); $dwooData->assign('title','Page Title');<br> <code>if(!isset($_SESSION['logged'])){</code> <code>$dwooData-&gt;assign('logged',0);</code> <code>$dwoo-&gt;output($dwooTpl,$dwooData);</code> <code>}else{</code> $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); }</p> <pre><code> $dwooData-&gt;assign('logged',1); $dwooData-&gt;assign('addLabel',0); $dwoo-&gt;output($dwooTpl,$dwooData); }else{ $labelName = mysql_real_escape_string($_POST['labelName']); $labelName = trim($labelName); if(strlen($labelName) &lt; 1) { $dwooData-&gt;assign('ERR1','Label Name is not valid. You must enter at least 1 letter as Label 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-&gt;assign('ERR2',"Label '&lt;strong&gt;$labelName&lt;/strong&gt;' already exists."); }else{ $sql = "INSERT INTO label VALUES('','$userID','$labelName')"; mysql_query($sql) or die('Can\'t insert Label '. mysql_error()); $dwooData-&gt;assign('INFO','Label added Succesfully.'); } } $dwooData-&gt;assign('logged',1); $dwooData-&gt;assign('addLabel',1); $dwoo-&gt;output($dwooTpl,$dwooData); } </code></pre> <p>}</p> <p></pre></code></p> <p>Template Code <pre><code></p> <p>{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: <code>&lt;form method="POST" action="label.php"&gt;</code> <code>Name: &lt;input type="text" name="labelName"&gt;&lt;br&gt;</code> <code>&lt;input type="submit" value="add" name="addLabel"&gt;</code> <code>&lt;/form&gt;</code> {foreach $labels label} {escape($label)} {/foreach} {/if} {else} You must login to access this page. {/if} {include(file='footer.tpl.html')}</p> <p></pre></code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload