Bug report

5 views
Skip to first unread message

Radek

unread,
Feb 26, 2011, 6:40:56 PM2/26/11
to kuwata-lab-products
Hi makoto kuwata

I found a strange behavior.

What's wrong on following code

#{ {True: 'checked', False: 'unchecked'}[role in user.roles] }

In ipython it looks good. But in Tenjin I got an error.
_buf.extend((''' />
''', to_str( {True: 'checked', False: 'unchecked'), '''[role in
user.roles] }

Is there any workarround?

My first code was:
<?py if role in user.roles: ?>
checked
<?py #endif ?>
but I like to be more compact
#{ {True: 'checked', False: ''}[role in user.roles] }

Makoto Kuwata

unread,
Feb 26, 2011, 7:32:30 PM2/26/11
to kuwata-la...@googlegroups.com
Hi Radek,

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

Radek

unread,
Feb 27, 2011, 2:00:56 AM2/27/11
to kuwata-lab-products
hi makoto

On 27 ún, 01:32, Makoto Kuwata <k...@kuwata-lab.com> wrote:
> 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] =}

Thank you, now I rewrote it to:

${role}<input type="checkbox" name="roles" value="${role}"
{= {True: 'checked', False: ''}[role in user.roles] =} />

instead old version:

${role}<input type="checkbox" name="roles" value="${role}"
<?py if role in user.roles: ?>
checked
<?py #endif ?>
/>

I just upgraded PyTenjin yesterday to 1.0.1. But didn't check the
documentation, my mistake.


So, thanks for help.

Makoto Kuwata

unread,
Feb 27, 2011, 5:14:36 AM2/27/11
to kuwata-la...@googlegroups.com
On Sun, Feb 27, 2011 at 4:00 PM, Radek <radek....@gmail.com> wrote:
>
> Thank you, now I rewrote it to:
>
> ${role}<input type="checkbox" name="roles" value="${role}"
> {= {True: 'checked', False: ''}[role in user.roles] =} />
>

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

Makoto Kuwata

unread,
Feb 27, 2011, 8:54:28 AM2/27/11
to kuwata-la...@googlegroups.com

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

Reply all
Reply to author
Forward
0 new messages