pyramidtemplate-talchameleon

Tal condition string contains


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.


Solution

  • 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>