2

i have 2 fields (fieldA and fieldB)

what i want : - if the fieldA contains something then the fieldB should not be displayed

what i try :

<span tal:replace="here/getFieldA" />

<span tal:omit-tag="here/getFieldA"  tal:replace="here/getFieldB" />

so it doesn't work

thanks for your help

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
bklups
  • 101
  • 1

3 Answers3

4

What you are looking for is tal:condition, possibly combined with the not: and exists: prefixes:

<span tal:replace="here/getFieldA" />

<span tal:condition="not:exists:here/getFieldA" tal:replace="here/getFieldB" />

Alternatively, you can use the | operator, which acts like an if operator, testing existence of the first element. If it doesn't exist, it'll use the next expression, and so on:

<span tal:replace="here/getFieldA | here/getFieldB" />

The tal:omit-tag attribute means something very different. If it's expression evaluates to True, then the tag, and only the tag itself, is omitted from the output. This is best illustrated with an example:

<span tal:omit-tag="">
    <i>This part will be retained</i>
</span>

Rendering that piece of pagetemplate results in:

<i>This part will be retained</i>

The surrounding <span> tag was omitted, but the contents were preserved.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

Try

<span tal:condition="here/getFieldA"  tal:replace="here/getFieldB" />

The Zope Page Templates Reference http://docs.zope.org/zope2/zope2book/AppendixC.html

1

This is a refinement of the original answer, based on the comments:

<tal:block 
    tal:define="zone here/getZoneintervention;
                thezone python:', '.join(zone);
                dep here/getDepartements;
                thedep python:', '.join(dep)">

   <span tal:condition="zone" tal:replace="thezone" /> 
   <span tal:condition="not:zone" tal:replace="thedep" /> 

</tal:block>
SteveM
  • 6,058
  • 1
  • 16
  • 20
  • `or` is not the right description for the pipe operator. It only looks at the next element if the element before the `|` is not found (so raises `NotFound`, `AttributeError` or `KeyError`. If the expression before the pipe returns a boolean `False` (or equivalents such as `None` or `[]` or `0`) then that's what gets returned! – Martijn Pieters Mar 09 '11 at 19:00
  • I recently had to help someone who had misunderstood the nature of the operator, because of incorrect interpretation. :-) – Martijn Pieters Mar 11 '11 at 08:03
  • here is my complete code : ` ` if the field Zoneintervention is empty nothing is displayed after – bklups Mar 11 '11 at 08:46
  • Martijn's comment explains why that happens. My example was dependent on here/getFieldA actually failing, not just returning false or an empty string. tal:condition combined with the "not:" preface should be your solution. If getZoneintervention always returns something, don't use "exists:" in your tests. – SteveM Mar 11 '11 at 15:49