Embedding python variables as html tag attirbutes

5 views
Skip to first unread message

vimal

unread,
Nov 14, 2007, 6:14:14 AM11/14/07
to TurboGears
hi all,

i have a button and i wanted it to enable or
disable with respect to a python variable.

code:

<?python
if usr_rght in user_rights: //usr_rght is an
integer and user_rights is a list//
add_user = "false"
else:
add_user = "true"
?>

<input type="button" value="ADD USER" disabled="${add_user}" />

when i use simply ${add_user} it prints-------------- false

Marco Mariani

unread,
Nov 14, 2007, 6:51:25 AM11/14/07
to turbo...@googlegroups.com
vimal wrote:
> <?python
> if usr_rght in user_rights: //usr_rght is an
> integer and user_rights is a list//
> add_user = "false"
> else:
> add_user = "true"
> ?>
>

If you can avoid <? snippers, please do yourself a favour.

> <input type="button" value="ADD USER" disabled="${add_user}" />
>
> when i use simply ${add_user} it prints-------------- false
>

You must user None if you don't want to render the attribute.


With python 2.5:

<input type="button" value="ADD USER" disabled="${None if usr_rght in
user_rights else 'DISABLED'}" />

With python 2.3-2.4:

<input type="button" value="ADD USER"

disabled="${['DISABLED',None][usr_rght in user_rights]}" />

or

<input type="button" value="ADD USER" disabled="${usr_rght not in
user_rights and 'DISABLED' or None}" />


Reply all
Reply to author
Forward
0 new messages