Mako templates inline if statement

ensnare picture ensnare · May 15, 2010 · Viewed 8.3k times · Source

I have a template variable, c.is_friend, that I would like to use to determine whether or not a class is applied. For example:

if c.is_friend is True
<a href="#" class="friend">link</a>

if c.is_friend is False
<a href="#">link</a>

Is there some way to do this inline, like:

<a href="#" ${if c.is_friend is True}class="friend"{/if}>link</a>

Or something like that?

Answer

Jochen Ritzel picture Jochen Ritzel · May 16, 2010

Python's normal inline if works:

<a href="#" ${'class="friend"' if c.is_friend else ''}>link</a>