views:

16

answers:

1

I'm creating a form elements template file in PHPTAL. I would like to be able to OPTIONALLY pass in an id attribute for a field...

So far the code looks like this:

<xml>
  <tal:block metal:define-macro="text">
    <label tal:condition="php: !isset(hideLabel) || isset(hideLabel) && !hideLabel">${field/label}</label>
    <input name="${name}" type="text" value="${field/value}" />
    <p tal:condition="exists:field/error">${field/error}</p>
  </tal:block>
</xml>

This works as advertised. What I'd like to add is something, like

<input name="${name}" tal:attributes="id exists: id $id | $name" value="${field/value}" />

to allow me to optionally pass in an id from the METAL call...

Should I be doing it differently? I've tried using PHP: isset(id) ? $id : NULL and variations thereof, but just end up with an id="0" in the resultant HTML.

Any ideas?

A: 

In case anyone else needs it, one working answer is:

<xml>
  <tal:block metal:define-macro="text">
    <label tal:condition="not: exists:hideLabel">${field/label}</label>
    <input name="${name}" tal:attributes="id id | nothing" type="text" value="${field/value}" />
    <p tal:condition="exists:field/error">${field/error}</p>
  </tal:block>
</xml>

Where passed in variables are id, name, an array named field, and hideLabel .

Note, that I've also managed to simplify the label test to something which I believe is more idiomatically TAL.

Dycey