That's how I got it to work, but without the Tales-Keyword. I just added a IF statement to my SQL statement that retrieved the categories. Kind of hackish, if you ask me, but it works! Thanks for all your replies!

----- Original Message -----
Date: Tue, 28 Apr 2009 12:45:43 +0200
From: Julian Haupt <>
Subject: Re: [PHPTAL] Selected attribute in select list
Message-ID: <>
Content-Type: text/plain


The way you USA should work, but try this:

<tal:block tal:repeat="c categories">
     <option tal:attributes="value c/id;selected php: f.categoryid ==" tal:content="c/category"></option>

In general it might be a bad idea to use php code and explicit
comparisons in the templates. This is a bad practice which entangles the
view with the underlying business logic.

I've added a new TALES-Keyword for this purpose: condition

It works like mysql's IF statement. If the expression in the first block
is true, execute the next block, otherwise the last block.

function phptal_tales_condition($expression, $nothrow) {
       // split OR expressions
       $exps = preg_split('/\s*\|\s*/sm', $expression);

       if (!isset($exps[1])) {
               $exps[1] = 'nothing';

       if (!isset($exps[2])) {
               $exps[2] = 'nothing';

       return '(' . phptal_tales($exps[0], $nothrow) . ') ? ' .
phptal_tales($exps[1], $nothrow) . ' : '. phptal_tales($exps[2],

It might not be perfect, because it uses TALES' "|" operator for a
completely different operation.

Please note: as you cannot do explicit comparisons with this keyword,
you must define the result of the comparison in your model.

With the help of the condition keyword you can write the above template
like this:

<tal:block tal:repeat="c categories">
   <option tal:attributes="value c/id; selected condition:|
string:selected|not:true" tal:content="c/category"></option>

The "active" property might be determined like this:

$id = (int)$_REQUEST["id"]; // get the id in the url query

$formSQL = "SELECT, categoryid, category, title, publication,
DATE_FORMAT(date, '%Y/%m/%d') AS fDate, author, summary, filename,
IF($id,1,NULL) AS active FROM `resources-items` AS i LEFT JOIN
`resources-categories` AS c ON WHERE$id"; // get
the row to be edited

$t = new PHPTAL("test.html");
$t->categories = getRows("SELECT id, category FROM
`resources-categories` ORDER BY category"); // get all categories
$t->form = getRows($formSQL);


Julian Haupt

PHPTAL mailing list

Reply via email to