On Sun, Feb 27, 2011 at 8:40 AM, Radek <radek....@gmail.com> wrote:
> Hi makoto kuwata
>
> I found a strange behavior.
>
> What's wrong on following code
>
> #{ {True: 'checked', False: 'unchecked'}[role in user.roles] }
>
It is not allowed to include '}' inside of ${...}. This is a
limitation of pyTenjin.
Please try:
<?py d = {True: 'checked', False: 'unchecked'} ?>
#{d[role in user.roles]}
Or if you use pyTenjin 1.0.1:
{= {True: 'checked', False: 'unchecked'}[role in user.roles] =}
--
regards,
makoto kuwata
Tenjin provides HTML helper which prints 'checked="checked"'.
## main program
from tenjin.html import *
## html template
<input type="checkbox" #{checked(role in user.roles)} />
--
regards,
makoto kuwata
In addition, tenjin.html.nv() makes your code shorter.
<input type="checkbox" #{nv('role', role, checked=(role in user.roles))} />
Output:
## if (role in user.roles) == True:
<input type="checkbox" name="role" value="..." checked="checked" />
## else:
<input type="checkbox" name="role" value="..." />
--
regards,
makoto kuwata