1

Trying to change table data based on conditions.

<td tal:condition="string.stringname != '-shadow'"><strong>Stuff</strong></td>
<td tal:condition="string.stringname == '-shadow'"><em>Stuff</em></td>

string.stringname may have -shadow at the very end of the string, and it may not. I'm trying to get tal to display either table data based on whether one or the other is true. The page will need to display both cases, if both cases are met of course. tal:condition doesnt seem to be able to search if a string contains something, only if something is explicitly true or false.

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

1 Answers1

1

Use str.endswith() to test if a string ends with a given substring:

<td tal:condition="not string.stringname.endswith('-shadow')"><strong>Stuff</strong></td>
<td tal:condition="string.stringname.endswith('-shadow')"><em>Stuff</em></td>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Perfect. Exactly what I needed. I'm curious though, and this may come up. Is there a way to do this with text in the middle of the string? I know python can with `in`, but I don't believe you can with this template. Example, `otherstring.stringname` might end with `-shadow1396378908` the numbers denoting unix timestamp. No way to know what the timestamp is, but I may need to use `tal:condition` the same way, and it won't end with `-shadow`. Thoughts? – Jer_TX Apr 01 '14 at 19:02
  • Just use `in`; these are Python expressions, so just use Python: `'-shadow' not in string.stringname` and `'-shadow' in string.stringname` – Martijn Pieters Apr 01 '14 at 19:04
  • Hm. I tried that (or some variation) and it didn't seem to work. I suppose if it arises I'll play around with it. Thanks again, I was googling for a good hour and a half >. – Jer_TX Apr 01 '14 at 19:17