Juparave schrieb:
> I'm having trouble accessing my session variables inside genshi
> templates.
>
> Somewhere in code I have this statement.
>
> session['cartitems'] = len(cart.items)
>
> That stores in a session variable the number of items inside your
> cart.
>
> I call the variable this way inside the template
>
> <p>You have ${session['cartitems']} in your cart</p>
>
> And works well, but if a start a new session I get this error.
>
> UndefinedError: {} has no member named "cartitems"
>
> so I modified the code to look like this
>
> <py:if test="hasattr(session, 'cartitems')">
> <p>You have ${session['cartitems']} in your cart</p>
> </py:if>
>
> and always resolves to false, cause I never get to see how many items
> I have inside my cart anymore. The same happens with:
>
> <py:if test="defined(session['cartitems'])">
> <p>You have ${session['cartitems']} in your cart</p>
> </py:if>
>
> ${value_of(session['cartitems'], default='0'} always returns '0'
>
> I don't know how the tests are performed, but some help will be nice.
Session is a dict-like object. You test for *attributes* instead of
membership. Which of course fails.
>>> d = {"foo" : "bar"}
>>> hasattr(d, "foo")
False
>>> "foo" in d
True
And I'd not store the length in there anyway. Store the cartitems
directly, and then compute the len on the fly. Less clutter, and no
duplication of data when you need the actual items.
Diez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---