Hi Levi

I'm posting an extract from one of my previous posts, I think it might give you an additional perspective to get out of the "tal:conditional" grip in your template code..?

--- Begin Quote ---

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>

--- End Quote ---


Levi Stanley wrote:
Was wondering if there was a better way to deal with radio buttons &
selection box in phptal.

One way, is make an array, and pass it the choices to the template.

Or you can do it like the method below, however I was wondering if there
is a better way than doing it like this?

<option value="">Choose Time</option>
<span tal:condition="exists: post/best_time_to_call">
<option tal:condition="php: post['best_time_to_call'] EQ 'Morning'"
selected="selected" value="Morning">Morning</option>
<option tal:condition="php: NOT post['best_time_to_call'] EQ 'Morning'"
value="Morning">Morning</option>
<option tal:condition="php: post['best_time_to_call'] EQ 'Evening'"
selected="selected" value="Evening">Evening</option>
<option tal:condition="php: NOT post['best_time_to_call'] EQ 'Evening'"
value="Evening">Evening</option>
</span>
<span tal:condition="not: exists: post/best_time_to_call">
<option value="Morning">Morning</option>
<option value="Evening">Evening</option>
</span>
</select>

Best regards,
Levi

_______________________________________________
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

Reply via email to