Hi Damien
I don't understand French, but I think I understand your problem by
looking at your code.
I agree that it is not a good idea to use php modifiers to set the
selected state of an option.
There are two ways that you can achieve the desired result:
1) The TAL-complaint way is to flag the selected state in your category
list, thus in your source object:
$categoryList = array(
...
array(
id => 123,
name => My Category,
selected => false
),
array(
id => 456,
name => My Other Category,
selected => true
),
...
);
Then your PHPTAL code is as simple as:
<select id="article_category" name="article_category">
<option
tal:repeat="category categoryList"
tal:attributes="selected category/selected; value category/id"
tal:content="category/name">
</option>
</select>
2) The other option (I sometimes need to use this approach) is to write
your own custom modifier, that compares the object's value to another
value. Note: This, however, is NOT TAL compliant and will make your
template code specific to PHPTAL and thus your templates will not be
portable to other TAL implementations.
Here is an example modifier:
function phptal_tales_match($src, $nothrow) {
$srcArr = explode(',', trim($src));
if (count($srcArr) == 2) {
return '(' . PHPTAL_TalesInternal::path($srcArr[0], $nothrow) .'
== '. PHPTAL_TalesInternal::path($srcArr[1], $nothrow) . ')';
} else {
return PHPTAL_TalesInternal::path($src, $nothrow);
}
}
This modifier, when included before you initiate a PHPTAL object, can be
used as follows:
<select id="article_category" name="article_category">
<option
tal:repeat="category categoryList"
tal:attributes="selected match:category/id,article/category;
value category/id"
tal:content="category/name">
</option>
</select>
So, when the value of article/category matches that of category/id, the
selected attribute will be set to "selected" for that specific option.
I hope this helps you in your quest.
Kind Regards,
Werner
Kind Regards,
Werner
Damien Louis wrote:
Bonjour, je suis actuellement en train de monter un site avec le coupe
Zend Framework et PHPTAL, et je bloque actuellement sur un "détail" du
côté du moteur de template qui me bloque à plusieurs endroits.
J'ai une table article et une table catégorie.
Pour chaque article est sélectionnée une catégorie. Donc, dans
l'enregistrement de l'article apparait l'ID de la catégorie.
Lors de la modification de l'article, j'envoi au formulaire l'objet
Article, qui a donc l'attribut catégorie, et l'objet Catégorie qui a
toutes les catégories.
Côté PHPTAL, j'ai donc un truc de ce style pour re-lister mes catégories.
Code :
<select name="categorie">
<option tal:repeat="categorieItem categoriesList"
tal:content="categorieItem/nom"
tal:attributes="categorieItem/id"></option>
</select>
Seulement, j'aimerai pouvoir sélectionner par défaut la catégorie de
l'article.
J'ai déjà utilisé PHPTAL sans Zend Framework, et je pouvais faire ceci :
Code :
<select name="categorie">
<option tal:repeat="categorieItem categoriesList"
tal:content="categorieItem/nom" tal:attributes="categorieItem/id;
selected php(categorieItem.id() == article.categorie())"></option>
</select>
Seulement, cette façon de faire est bancale puisque la méthode est
appelée depuis la vue, ce qui ne respecte pas la logique MVC puisque
la vue est censée afficher ce que lui envoi le contrôleur.
Bref, j'ai tenté cette approche avec Zend Framework, et ça ne passe pas.
Je suis donc à la recherche d'une solution plus propre, et surtout,
qui marche.
J'ai tenté un :
Code :
<select name="categorie">
<option tal:repeat="categorieItem categoriesList"
tal:content="categorieItem/nom" tal:attributes="categorieItem/id;
selected article/categorie"></option>
</select>
pensant que PHPTAL comprendrait que je veux "selected" que si la
catégorie de l'article correspond à la "value" de l'option de la
catégorie.
------------------------------------------------------------------------
_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal
_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal